[英]CakePHP model association deep model contain
我已經在這個問題上苦苦掙扎了一段時間,並且已經在我的CakePHP應用程序中與各種其他模型的關聯進行了匹配,但仍然無法使結果數組包含相關的ExpenseType。
一切都從屬性開始,定義為:
var $ recursive = 2;
var $ actsAs = array('Containable');
模型關聯
物業有很多比爾
比爾屬於物業
Bill hasOne ExpenseType
ExpenseType屬於Bill
在My PropetiesController中,我調用並分配給$ property:
$這 - >屬性 - > findById($ ID);
這導致:
Array
(
[Property] => Array
(
[id] => 2
[address] => **
[bedrooms] => 2
[bathrooms] => 1.5
[square_footage] => 973
[zip_code] => **
[created] => 2013-08-13 18:30:34
[modified] => 2013-08-15 19:08:32
)
[Bill] => Array
(
[0] => Array
(
[id] => 2
[expense_type_id] => 1
[property_id] => 2
[frequency] => monthly
[start_date] => 2013-09-16
[payee] => **
[amount] => **
[created] => 2013-08-20 19:57:41
[modified] => 2013-08-20 20:57:02
)
[1] => Array
(
[id] => 4
[expense_type_id] => 4
[property_id] => 2
[frequency] => monthly
[start_date] => 2013-09-15
[payee] => **
[amount] => 195
[created] => 2013-08-20 20:38:11
[modified] => 2013-08-20 20:38:11
)
)
)
對於我的生活,我無法讓數組包含Bill下的ExpenseType的詳細信息。 建議請!
更新:在模型上設置包含方法時,Cake現在報告:
模型“Bill”與模型“ExpenseType”無關
比爾模型
class Bill extends AppModel {
/*******************
* Variables *
*******************/
var $actsAs = array('Containable');
/*********************
* Model Associations *
**********************/
public $belongsTo = array('Property');
public $hasOne = array('ExpenseType');
}
費用類型模型
class ExpenseType extends AppModel {
/*******************
* Variables *
*******************/
var $actsAs = array('Containable');
/*******************
* Model Associations *
*******************/
public $belongsTo = array('Bill');
}
表結構
通過進行以下更改,我能夠使其工作:
物業模型
更改:
public $hasMany = array(
'Bill' => array(
'className' => 'bills',
'foreign_key' => 'property_id',
'dependent' => true
)
);
至:
public $ hasMany = array('Bill');
比爾模型
public $ belongsTo = array('Property','ExpenseType');
費用類型模型
public $ hasMany = array('Bill');
似乎Property模型中的無效關聯仍然允許查詢Bills,但不知何故搞砸了更深層次的關聯。 不知道為什么它會工作期間。
一些建議:
1.)在findById
之前遞歸遞歸到PropertiesController
$this-Property->recursive = 2;
2.)在Bill和ExpenseType模型上使用可包含的行為
var $actsAs = array('Containable');
...然后在PropertiesController中做...
$this->Property->contain(array(
'Bill' => array(
'ExpenseType'
)
));
$this->set('property', $this->Property->findById($id));
更新#1:
比爾模型
class Bill extends AppModel {
/*******************
* Variables *
*******************/
var $actsAs = array('Containable');
/*********************
* Model Associations *
**********************/
public $belongsTo = array(
'Property' => array(
'className' => 'Property',
'foreignKey' => 'property_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ExpenseType' => array(
'className' => 'ExpenseType',
'foreignKey' => 'expense_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
); // end belongsTo
} // end Bill model
費用類型模型
class ExpenseType extends AppModel {
/*******************
* Variables *
*******************/
var $actsAs = array('Containable');
/*******************
* Model Associations *
*******************/
public $hasMany = array(
'Bill' => array(
'className' => 'Bill',
'foreignKey' => 'expense_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
); // end hasMany
} // end ExpenseType model
然后清除緩存
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.