2011-08-23 111 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