我一直在努力解决这个问题,并且已经在我的CakePHP应用程序中与各种其他模型的关联进行了匹配,但仍然无法使结果数组包含相关的ExpenseType。
Everything始于Property,定义为:
var $ recursive = 2;
var $ actsAs = array('Containable');
模型关联
Property hasMany Bill
Bill belongsTo Property
Bill hasOne ExpenseType
ExpenseType belongsTo 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');
}
表格结构
答案 0 :(得分:3)
我能够通过进行以下更改来实现这一目标:
属性模型
更改:
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 :(得分:1)
一些建议:
1。)在findById
$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
然后清除缓存