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?
$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.
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.