CakePHP Model Model Association contains

I have been struggling with this problem for a long time and associating associations with other other Models in my CakePHP application, but still I can not get the result array to contain the associated ExpenseType.

It all starts with a property that is defined as:

var $ recursive = 2;

var $ actAs = array ('Containable');

Model associations

HasMany Bill Property

Bill owned property

Bill hasOne ExpenseType

ExpenseType owned by Bill


In My PropetiesController, I call and assign the $ property:

$ this-> Real Estate-> findById ($ ID);

This leads to:

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

Throughout my life, I cannot make an array contain ExpenseType details in Bill. Suggestions please!

UPDATE: when setting up the containment method on the model Cake now reports:

The Bill model is not associated with the ExpenseType model.

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

  • bills
    • ID
    • expense_type_id
    • PROPERTY_ID
    • ...
  • expense_types
    • ID
    • name
    • Description
+4
source share
2 answers

I managed to get this to work by making the following changes:

Property Model

Modified by:

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

To:

public $ hasMany = array ('Bill');

Bill model

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

ExpenseType Model

public $ hasMany = array ('Bill');

It seems that the incorrect association in the Property model still allows you to query for Bills, but somehow mixed up the deeper associations. Not sure why this will work.

+3
source

Some suggestions:

1.) Move the recursion in the PropertyController just before findById

 $this-Property->recursive = 2; 

2.) Use constrained behavior for the 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 the cache

+1
source

Source: https://habr.com/ru/post/1498449/


All Articles