CakePHP - the SQL log shows a working query which returns a row, but results variable is NULL?
So, here's my controller code:
public function admin_index(){
$clients = $this->paginate();
$this->set(compact('clients'));
}
And here's the relevant query from my SQL dump for this page:
SELECT `Client`.`id`, `Client`.`user_id`, `Client`.`first_name`, `Client`.`last_name`, `Client`.`company`, `Client`.`phone`, `Client`.`created`, `Client`.`modified`, (IF(LENGTH(company) > 0,company,CONCAT(first_name," ",last_name))) AS `Client__name` FROM `clients` AS `Client` WHERE 1 = 1 LIMIT 20
This query returns 1 r开发者_高级运维ow (under Num rows, 1 is displayed, just to be clear :).
In the view (or the controller), var_dump($clients) gives NULL
I can't understand why that is happening!
In the controller change:
$this->set(compact('clients'));
for
$this->set('clients', compact('clients'));
Because if not the variable in the view has no name, so you can't access it.
Well, I feel like a bit of a chump...
The offending code was actually in the model file, client.php.
public function afterFind(){
//todo
}
I was planning on writing some code here, but neglected to. afterFind needs to return an array of results. I wasn't doing so, hence all calls to Client::find (ie via ClientsController::paginate) were returning NULL, not even an empty array.
精彩评论