你好,隊友。嘗試在查找查詢中使用加入。
$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!'
)
).........
);
如果你試過的東西,請出示您的重複的方式嘗試過的東西,如'遞歸= 2'絕對應該包括關聯。這就是說:** http://stackoverflow.com/questions/29442675/containable-and-deep-associated-data** – ndm
發佈您目前用來生成'blog'數組的查詢。您應該使用'containable',而不是'遞歸= 2'作爲其極高ineffecient –
我的行爲是 公共職能視圖($ 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