2011-08-23 140 views
2

我有一个关系的建立是这样的:我怎样才能递归找到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 
        //... 
       ) 
      //... 
     ) 
) 

我如何也得为每个登录用户的数据?

回答

2

设置警告的recursive property为2

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

一个字:超过1设置递归到任何东西很容易臃肿的结果,即使在一个小的数据库。在这种情况下,它将为每个日志条目再次提取Geocache数据。您应该使用containable将结果限制为只有您需要的那些表和字段。

+0

只要将递归设置为1以上,我就会得到这个奇怪的错误:致命错误:无法在/var/www/ccache/app/models/geocache.php行中将字符串偏移量用作数组“你知道这可能是什么原因吗? – Ahtenus

+0

你能发表该文件的第94行(和环境)吗? – JJJ

+0

对不起,混淆了模型和控制器,并看着错误的文件。我会自己修复它,但无论如何感谢。 – Ahtenus