gpt4 book ai didi

php - CakePHP 模型关联深层模型包含

转载 作者:可可西里 更新时间:2023-10-31 23:06:23 25 4
gpt4 key购买 nike

一段时间以来,我一直在努力解决这个问题,并在我的 CakePHP 应用程序中匹配了与各种其他模型的关联,但仍然无法获得包含关联 ExpenseType 的结果数组。

一切始于属性,属性定义为:

var $recursive = 2;

var $actsAs = array('Containable');

模范协会

属性有很多账单

比尔属于属性(property)

Bill 有一个 ExpenseType

ExpenseType belongsTo Bill


在我的 PropetiesController 中,我调用并分配给 $property:

$this->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 现在报告:

Model "Bill" is not associated with model "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');
}

表结构

  • 账单
    • 编号
    • expense_type_id
    • 属性_id
    • ...
  • 费用类型
    • 编号
    • 姓名
    • 描述

最佳答案

通过进行以下更改,我能够使其正常工作:

属性模型

更改:

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,但不知何故搞乱了更深层次的关联。不确定为什么会有效。

关于php - CakePHP 模型关联深层模型包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18387610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com