How can i recursively find with cakePHP?
I have a relation set up like this:
class Geocache extends AppModel {
var $belongsTo = 'User';
var $hasMany = 'Log';
}
class Log extends AppModel {
var $belongsTo = array('User','Geocache');
}
class User extends AppModel {
var $hasMany = array('Geocache','Log');
}
and a $this->Geocache->find('first');
returns:
Array
(
[Geocache] => Array
(
[id] => 86
[user_id] => 3
[name] => Hejssa
//...
)
[User] => Array
(
[id] => 3
[us开发者_运维问答ername] => Ahtenus
//...
)
[Log] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 12
// How do i retrive the username and such from the User table together with this?
[geocache_id] => 86
//...
)
//...
)
)
How can I also get the User data for each log?
Set the recursive property to 2.
$this->Geocache->find(
'first',
array(
'recursive' => 2
)
);
A word of warning: setting recursive to anything over 1 will easily bloat the results even in a small database. In this case it will pull the Geocache data again for each log entry. You should use containable to limit the results to only those tables and fields that you need.
精彩评论