Running a Model::find in for loop in cakephp v1.3
Posted
by Gaurav Sharma
on Stack Overflow
See other posts from Stack Overflow
or by Gaurav Sharma
Published on 2010-05-27T05:49:23Z
Indexed on
2010/05/27
5:51 UTC
Read the original article
Hit count: 249
Hi all,
How can I achieve the following result in cakephp:
In my application a Topic is related to category, category is related to city and city is finally related to state
in other words:
topic belongs to category, category belongs to city , city belongs to state..
Now in the Topic controller's index action I want to find out all the topics and it's city and state.
How can I do this.
I can easily do this using a custom query ($this->Model->query() function ) but then I will be facing pagination difficulties.
I tried doing like this
function index()
{
$this->Topic->recursive = 0;
$topics = $this->paginate();
for($i=0; $i<count($topics);$i++)
{
$topics[$i]['City'] = $this->Topic->Category->City->find('all', array('conditions' => array('City.id' => $topics[$i]['Category']['city_id'])));
}
$this->set(compact('messages'));
}
The method that I have adopted is not a good one (running query in a loop)
Using the recursive property and setting it to highest value (2) will degrade performance and is not going to yield me state information.
How shall I solve this ?
Please help
Thanks
© Stack Overflow or respective owner