我試圖構建一個顯示用戶備忘錄表的視圖。備忘錄有一個類型(memo_type)和多個作者 - 作者表由記錄組成:id, memo_id, user_id, precedence
,MemoType爲id, memo_type_desc
。 (備忘錄也有一個所有者(Memo.user_id),但這是一個簡單的查詢,我有一個單獨的表,它工作正常)使用可包含,模型關聯查找()與深度關聯
這個想法是,當我查看有關用戶的詳細信息時,我顯示一個表備忘錄的用戶被列爲作者,並且包括具有遞歸= 3(詳見下文備忘錄型(字母,演示等)
所以,雖然我可以得到陣列I從一個讀(需要) ),這很慢並且檢索TON的數據,所以我試圖使用包含。 (我可以離開了memo_type_desc但最終用戶需要它。)
OK,所以我把public $actsAs = array('Containable');
在app_model.php
大的事情,這裏要注意的是,我得到相同的陣列後面不管MemoTypes的附加關聯級別 - 當我添加=> array('MemoTypes.memo_type_desc')
時,我在數組中看不到額外的級別。並且在第三級關聯中,我還得到一個錯誤:
警告(512):模型「備忘錄」未與模型「MemoTypes」關聯[CORE/cake/libs/model/behaviors/containable.php, line 343
模型關聯如下,這裏是user_controller中的View函數。任何想法我的備忘錄/ MemoTypes關聯或find()語法有什麼問題?
編輯:哦,我是一個蛋糕新手,我繼承了這個應用程序。 PS,該表由view.ctp中的foreach構建,但分頁肯定會更好,但不知道從哪裏開始。 = O
// Trying to get Containable method to work...
$this->set('userstuff', $this->User->find('all', array(
'conditions' => array('User.id' => $id),
'contain' => array(
'Author.user_id',
'Author.precedence',
'Author.memo_id' => array(
'Memo.memo_type_id' => array(
'MemoTypes.memo_type_id' // <--problem here?
)
)
)
)));
結果:
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1026
[group_id] => 2
[name] => John Doe
[username] => jd20001
[grpnum] => 99
[created] => 2010-03-22 15:08:35
[modified] => 2012-04-05 13:31:15
)
[Author] => Array
(
[0] => Array
(
[user_id] => 1026
[precedence] => 0
[memo_id] => 2579
[Memo] => Array
(
[memo_type_id] => 1
)
)
[1] => Array
(
[user_id] => 1026
[precedence] => 0
[memo_id] => 2583
[Memo] => Array
(
[memo_type_id] => 1
)
)
[2] => Array...
更多:
// Works, but retrieves entire users/memos tables so large memory footprint
$this->User->recursive = 3;
$this->set('user', $this->User->read(null, $id));
user.php的:
var $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id'
)
);
var $hasMany = array(
'Author' => array(
'className' => 'Author',
'foreignKey' => 'user_id',
'dependent' => true
),
'Memo' => array(
'className' => 'Memo',
'foreignKey' => 'user_id',
'dependent' => false
)
);
Memo.php:
var $hasMany = array(
'Author' => array(
'className' => 'Author',
'foreignKey' => 'memo_id',
'dependent' => true,
'order' => 'precedence'
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MemoType' => array(
'className' => 'MemoType',
'foreignKey' => 'memo_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Memo_type.php:
var $hasMany = array(
'Memo' => array(
'className' => 'Memo',
'foreignKey' => 'memo_type_id',
'dependent' => false
)
);