我怎么能用cakePHP递归找到?

时间:2011-08-23 14:31:05

标签: cakephp

我的关系设置如下:

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');
}

$this->Geocache->find('first');返回:

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
                    //...
                )
            //...
        )
)

如何获取每个日志的用户数据?

1 个答案:

答案 0 :(得分:2)

recursive property设为2。

$this->Geocache->find(
    'first',
    array(
        'recursive' => 2
    )
);

警告:即使在小型数据库中,设置递归到超过1的任何内容也会轻易导致结果膨胀。在这种情况下,它将为每个日志条目再次提取Geocache数据。您应该使用containable将结果限制为仅需要的那些表和字段。