CakePHP Retrieving data from custom model
I got few models with custom queries, just don't want to fight with them so I used $this->query
met开发者_StackOverflow社区hod to query DB.
Now got question...
The only way I can access data is using $model[0][0]['field']
, is there any other way to display them in foreach
loop?
When writing your queries, follow the CakePHP naming conventions - this makes the returned data much easier to handle.
SELECT * FROM `posts` as Post, `comments` as `Comment` ...
Then you get data returned that looks very similar to what you would get from a find()
, i.e. $queryResults['ModelName']['FieldName]
or $queryResults[0]['ModelName']['FieldName]
.
This link might help too. It relates to use of find
rather than query
, but it shows how you can create a method in your AppModel
to automatically reformat returned data (moving computed data into a model, to avoid having returned values at [0][0]).
http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/
avoid using query function if you can. $this->Model->find()
makes thing makes things cleaner.
精彩评论