YII: Call to a member function offset() on a non-object - php

I am very new to php and yii2, i am trying to read all data records by active record
The code below give error Call to a member function offset() on a non-object
$cmylist = ClassInfo::find()->all();
$pages = new Pagination(['totalCount' => count($cmylist)]);
$models = $cmylist->offset($pages->offset)->limit($pages->limit)->all();
Seems cmylist is a array and i can not call offset and count on it
This is really made me crazy, thanks for your help

Of course, if You call all() it's create final result. Call ->offset() before ->all()
just see
http://www.yiiframework.com/doc-2.0/yii-data-pagination.html

Calling all() returns array of result models, You need to adjust Your code this way:
$cmylist = ClassInfo::find(); //activeQuery instance
$pages = new Pagination(['totalCount' => $cmylist->count()]);
$models = $cmylist->offset($pages->offset)->limit($pages->limit)->all();

Related

Putting a limit on doctrine queries

I'm working on a site to make an inventory of series.
public function category(string $categoryName): Response
{
$categoryInfos = $this->getDoctrine()
->getRepository(Category::class)
->findOneByName($categoryName);
$programs = $this->getDoctrine()
->getRepository(Program::class)
->findByCategory($categoryInfos);
return $this->render('wild/category.html.twig', ['programs' => $programs, 'category' => $categoryInfos]);
}
This function allows me to retrieve all programs belonging to the specified category.
I would now like to limit the number of programs to 3 in my request.
$programs = $this->getDoctrine()
->getRepository(Program::class)
->findByCategory($categoryInfos)
->setMaxResults(3);
But this shows me the error:
-> Call to a member function setMaxResults() on array
What did I do wrong?
I get lost in the symfony doc, being new ^^
You are calling a method inside repository class that returns an array and you cannot execute setMaxResults() on arrays.
If you want to set the size of result you should set it inside findByCategory() method in ProgramRepository.php

how to get num_rows() on a non-object in CI

so i'd like to get the response from my model:
function get_list_sales_kit(){
$brispot = $this->load->database('brispot',TRUE);
$brispot->select('id,title,imgurl,description');
$qrydata = $brispot->get('saleskit');
$brispot->close();
return $qrydata->result();
}
and this is the function to call that model:
function salesKit2($request){
$result = new stdClass;
$user='';
$CI =& get_instance();
$CI->load->library('libs_bearer');
$CI->load->library('libs_brispot');
$CI->load->model('service_model');
$datapost = json_decode($request);
if(isset($datapost->user)){
$user = substr('00000000'.$CI->security->xss_clean(trim($datapost->user)),-8);
if($CI->libs_bearer->cekToken($user)==true){
$getdata = $CI->service_model->get_list_sales_kit();
if($getdata->num_rows()>0){
$result->responseCode='00';
$result->responseDesc='Inquiry berhasil.';
$result->responseData=$getdata->result();
}
}
but i got eror result like fatal eror Call to a member function num_rows() on a non-object.
i confused how to call non object on num_row, or is there anything to replace num_row to get the response?
If you really want to return the result from your get_list_sales_kit() function the only way to get the number of rows is to count($getdata) as Lawrence said in the comments.
With that being said, I sometimes return the database object itself like so:
function get_list_sales_kit(){
$brispot = $this->load->database('brispot',TRUE);
$brispot->select('id,title,imgurl,description');
$qrydata = $brispot->get('saleskit');
$brispot->close();
return $qrydata; // this line changed
}
as such you can run any CI database functions on it like row() result() num_rows() and so on. Nothing has to be changed in the second code snippet as for some reason you are already accessing the result() object even though you are returning it in the first function.
Note: I'm not sure if close() will affect the objects being called after the fact. Why are you closing the database after doing one thing?

Laravel dynamic queries on array of Eloquent models

I am currently trying to make a function that calls different scopeQueries such as scopeByLocation() or scopeByPublished() on models defined in an array. I've got the basics working through [this link][1]. However, when trying to access custom made query scopes that are defined in the corresponding model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".
What I want to achieve is a single method which loops through every model in the array of models and retrieves & calls the right scopeQuery on the model, something like this:
$modelElements = $model::{$queryScope}();
Where for example $model = 'Modules\News\Models\Article'
And $queryScope is a defined queryScope in the model itself. E.g. scopeForLocation($location).
I've tested $queryScope = 'all' and I get a result just fine, however when I try to access a custom queryScope ($queryScope = 'ForLocation($location)->get') that exists within for example the Location model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".
So this all happens in a foreach-loop where every model in my models-array gets called and then the corresponding queryScope gets called on the model.
Why does the $queryScope = 'all' method works on my dynamic models, but other scopes throw an error? I really hope someone could help me get into the right direction with this issue.
Thanks in advance,
J. Doe.
Okay, I've finally solved it the following way:
//array of models
public function models()
{
return [
'Modules\Website\Models\Article',
...
];
}
//function that retrieves all elements for a model
public function getAllElementsForModel($model, $param)
{
//instantiate model
$model = new $model;
//call queryScope
//'queryScope' could be any queryScope that is defined within your model(s),
//the parameters are needed for the associated queryScope
$query = call_user_func_array([$model, 'queryScope'], [$param1, $param2]);
$result = $query->get();
//do stuff with your $result
}
//retrieves all
public function all($param)
{
//loop through the array of models
foreach($this->models() as $model){
$this->getAllElementsForModel($model, $param);
//do stuff here...
}
}
Sharing is caring!

Method save does not exist use get()

public function postMoviesEdit( MoviesEditRequest $request, $id){
$episode= episode::where('movies_id', $id)->get();
$episode->ep_quality = $request->txtQuality;
$episode->ep_url = $request->txtFull;
$episode->updated_at = new DateTime();
$episode->save();
I get that message: Method save does not exist.
I used the get () method instead of using first (). I want to get more than one result, so I used the get () method. Is there a way to fix it? Kindly help me so i can get more of a result

Laravel Manual Pagination Error

I have a issue related to Laravel5 Manual Pagination.
I need to process an array of query builder result and some other arrays and make a pagination for that.
controller.
public function searchCategory($id){
$arr = DB::table('business_businesscatagories')->lists('fk_business_id');
return Paginator::make($arr, count($arr), 2);
}
Namespaces..
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
But I got the error like below..
FatalErrorException in IndexController.php line 122:
Call to undefined method Illuminate\Pagination\LengthAwarePaginator::make()
How can I solve this issue....
I examined Illuminate\Pagination\LengthAwarePaginator and was able to confirm that it doesn't have make method.
However you can achieve this by using its constructor method.
__construct(mixed $items, int $perPage, int|null $currentPage = null, array $options = array())
Since you already have the correct namespace specified:
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
You could simply do this:
$paginator = new Paginator($items, $total, $per_page);

Categories