views:

181

answers:

2

Hi,

I got few models with custom queries, just don't wanna fight with them so I used $this->query method 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?

Cheers!

A: 

avoid using query function if you can. $this->Model->find() makes thing makes things cleaner.

Funky Dude
A: 

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/

Roy
Thanks Roy, the link you gave me made things cleaner for me!
Sergio E.