I’m a little lost creating an instance of a model. Gii will create the MVC for a given table (Job in this case) and gather the records like so:
$model = new Job('search');
This will then call the view and use the $model variable to populate the page. However if I create a criteria and then instantiate a model with this criteria instead of ‘search’ the view fails to load with the error:
Illegal offset type in isset or empty
The criteria and model and declared as follows:
$criteria = new CDbCriteria;
$criteria->select = 't.*';
$criteria->join = 'LEFT JOIN PrinterHasJob phj ON phj.jobId=t.id';
$criteria->condition = 'phj.officeId=:officeId';
$criteria->params = array(':officeId'=>$this->currentUser->officeId);
$model = new Job($criteria);
How should I be going about this? What is missing?
you can use the criteria object with a find method like this: Job::model()->findAll($criteria);
$model = Job::model()->find($criteria);
// or $models = Job::model()->findAll($criteria);
Probably you misunderstand the work flow of gii-generated admin page and the search() method in Job model.
$model = new Job('search');
This is not the retrieved data that is displayed in the grid.
It’s a holder of the search parameters.
Job::search() will use its contents to create a criteria, and it will return an instance of CActiveDataProvider for the grid. And the actual data retrieved from db will be supplied as an array of Job objects to the grid by the data provider.
Thanks for the responses. This has partially solved my problem.
This works and it will return one result with Job::model()->find($criteria) as expected. However when I try with findAll() to display all the results in the view, I find that $model is now an array rather than an object and therefore the view cannot display any results.
CActiveRecord::findAll() returns an array of AR objects. It’s the expected behavior.
You have to loop through the array like this:
foreach($models as $model)
echo '<p>' . CHtml::encode($model->name) . '</p>';
But I’d rather recommend you to try CListView with CActiveDataProvider.
CListView is used in ‘index’ page of gii-generated CURD.
And also you can easily replace the CGridView of admin page with a CListView, if you want to filter the output.
I understand that but I want to replace the standard:
to filter the results with the desired criteria and leave the model intact for the view.
I initially tried doing the same by defining the values using the relationship but like so
$model->selectedPrinters->officeId = $this->currentUser->officeId;
but it returns the error:
Indirect modification of overloaded property Job::$selectedPrinters has no effect
I’ve looked into this error and it seems I can get around it by upgrading to PHP 5.4 but I prefer not to do this for various reasons.
So I’m stuck here trying to find the best solution.
Did you have a look at this wiki?
It will tell you how to filter the output by the related model attributes.
I broke PHP trying to upgrade to PHP 5.4 on my laptop running Kubuntu and Nginx. I knew it was a mistake.
I will look into your last response in the morning after I fix everything. I don’t think I can be much more productive tonight.
Thanks again for your help.
I followed that little tutorial but the generated query did not suit my needs. I checked the MySQL query log to check the generated query and found that it adds conditions I don’t want (like status=1 and archived=0) and distinct on one column.
I have no more time to spend on this so going back to one of the other solutions.