私はこの問題にしばらく苦労しており、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
- 名前
- 説明