Ahtenus
Ahtenus

Reputation: 595

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
            [username] => 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?

Upvotes: 2

Views: 2707

Answers (1)

JJJ
JJJ

Reputation: 33163

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.

Upvotes: 2

Related Questions