首页 文章

CakePHP模型关联深层模型包含

提问于
浏览
4

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

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

var $ recursive = 2; var $ actsAs = array('Containable');

Model Associations

properties 有很多比尔

比尔属于 properties

Bill hasOne ExpenseType

ExpenseType属于Bill


在My 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”无关

Bill Model

class Bill extends AppModel {
        /*******************
        * Variables        *
        *******************/
        var $actsAs = array('Containable');

        /*********************
        * Model Associations *
        **********************/
        public $belongsTo = array('Property');
        public $hasOne = array('ExpenseType');
    }

ExpenseType Model

class ExpenseType extends AppModel {
    /*******************
    * Variables        *
    *******************/
    var $actsAs = array('Containable');

    /*******************
    * Model Associations      *
    *******************/
    public $belongsTo = array('Bill');
}

Table Structure

  • 账单

  • id

  • expense_type_id

  • property_id

  • ......

  • expense_types

  • id

  • 名字

  • 说明

2 回答

  • 1

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

    Property Model

    更改:

    public $hasMany = array(
            'Bill' => array(
                'className' => 'bills',
                'foreign_key' => 'property_id',
                'dependent' => true
                )
            );
    

    至:

    public $ hasMany = array('Bill');

    Bill Model

    public $ belongsTo = array('Property','ExpenseType');

    ExpenseType Model

    public $ hasMany = array('Bill');

    似乎Property模型中的无效关联仍然允许查询Bills,但不知何故搞砸了更深层次的关联 . 不知道为什么它会工作期间 .

  • 3

    一些建议:

    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));
    

    UPDATE #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
    

    然后清除缓存

相关问题