4

我已经为这个问题苦苦挣扎了一段时间,并且已经将关联与我的 CakePHP 应用程序中的各种其他模型进行了匹配,但仍然无法让结果数组包含相关的 ExpenseType。

一切从属性开始,属性定义为:

var $recursive = 2;

var $actsAs = array('Containable');

模型协会

物业有很多账单

Bill belongsTo 属性

Bill hasOne ExpenseType

ExpenseType 属于 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 现在报告:

模型“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');
}

表结构

  • 账单
    • ID
    • 费用类型ID
    • property_id
    • ...
  • 费用类型
    • ID
    • 姓名
    • 描述
4

2 回答 2

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,但不知何故弄乱了更深层次的关联。不知道为什么它会起作用。

于 2013-08-22T19:36:29.847 回答
1

Some suggestions:

1.) Move recursive to PropertiesController right before findById

$this-Property->recursive = 2;

2.) Use containable behavior on Bill and ExpenseType models

var $actsAs = array('Containable');

...then in PropertiesController do...

$this->Property->contain(array(
    'Bill' => array(
        'ExpenseType'
    )
));

$this->set('property', $this->Property->findById($id));

UPDATE #1:

Bill Model

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

ExpenseType 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

then clear cache

于 2013-08-22T18:49:11.103 回答