4

私はこの問題にしばらく苦労しており、CakePHP アプリケーションで他のさまざまなモデルとの関連付けを照合しましたが、結果の配列に関連付けられた ExpenseType を含めることができません。

すべては次のように定義される Property で始まります。

var $recursive = 2;

var $actsAs = array('Containable');

モデルの関連付け

プロパティ hasMany Bill

Bill belongsTo プロパティ

Bill hasOne ExpenseType

ExpenseType 所属請求書


My PropertiesController で、$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');
    }

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

ExpenseType モデル

public $hasMany = array('Bill');

Property モデルの無効な関連付けにより、Bills のクエリが許可されていたようですが、どういうわけかより深い関連付けが台無しになりました。なぜそれが機能するのかわかりません。

于 2013-08-22T19:36:29.847 に答える
1

いくつかの提案:

1.) 直前の PropertiesController に再帰的に移動します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

ExpenseType モデル

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

次にキャッシュをクリアします

于 2013-08-22T18:49:11.103 に答える