2016-07-24 41 views
0

我有一個博客,用戶可以喜歡和評論他們。對於博客視圖操作,我將列出此博客條目的所有評論。直到現在我能列出所有的評論,但我想顯示用戶名,不幸的是我只有在評論對象中的User_id。我該怎麼辦?我已經嘗試了遞歸= 2Cakephp包含更深的關聯

Here is a trace of the variable:

這是給一個誤差模型 「Rmcomment」 不與模型 「用戶」 關聯[APP /供應商/ cakephp中/ cakephp中/ LIB /蛋糕/型號/行爲/ContainableBehavior.php,第342行],而只有最後一級查詢不起作用

+0

如果你試過的東西,請出示您的重複的方式嘗試過的東西,如'遞歸= 2'絕對應該包括關聯。這就是說:** http://stackoverflow.com/questions/29442675/containable-and-deep-associated-data** – ndm

+0

發佈您目前用來生成'blog'數組的查詢。您應該使用'containable',而不是'遞歸= 2'作爲其極高ineffecient –

+0

我的行爲是 公共職能視圖($ ID = NULL){\t \t $這個 - > Blog->遞歸= 2; \t \t如果(!$這個 - > Blog->存在($ ID)){ \t \t拋出新NotFoundException(__ d( 'croogo', '無效%s',__d( 'RM', '博客') )); \t} \t \t $ options = array('conditions'=> array('Blog。'。$ this-> Blog-> primaryKey => $ id)); (''blog',$ this-> Blog-> find('first',$ options)); } – benone

回答

0

你好,隊友。嘗試在查找查詢中使用加入

$this->Rmcomment->find('all', array(
'joins' => array(
     array(
     'table' => {{ya users table name}}, 
     'alias' => 'User', 
     'type' => 'LEFT', 
     'conditions' => array('User.id = Rmcomment.user_id') 
    ), 
    'fields' => array(
    'Rmcomment.something', 
    'Rmcomment.somethingelse', 
    'User.name' //Here comes your name from users table using the user_id from comments 
    ), 
    'group' => 'Rmcomment.id' 
) 
) 
); 

它應該返回:

array(
    array(
     'Rmcomment' => array(
      '!fields from Rmcomment!' 
     ), 
     'User' => array(
      '!fields from User!' 
     ) 
    ), 
    array(
     'Rmcomment' => array(
      '!fields from Rmcomment!' 
     ), 
     'User' => array(
      '!fields from User!' 
     ) 
    )......... 
);