Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP model association deep model contain
    text
    copied!<p>I've been struggling with this problem for some time now, and have matched the associations with various other Models in my CakePHP applicaiton, but still can not get the result array to contain the associated ExpenseType.</p> <p>Everything Starts with Property, which is defined as:</p> <blockquote> <p>var $recursive = 2;</p> <p>var $actsAs = array('Containable');</p> </blockquote> <p><strong>Model Associations</strong></p> <p>Property hasMany Bill</p> <p>Bill belongsTo Property</p> <p>Bill hasOne ExpenseType</p> <p>ExpenseType belongsTo Bill</p> <hr> <p>In My PropetiesController, I call and assign to $property:</p> <blockquote> <p>$this->Property->findById($id);</p> </blockquote> <p>This results in:</p> <pre><code>Array ( [Property] =&gt; Array ( [id] =&gt; 2 [address] =&gt; ** [bedrooms] =&gt; 2 [bathrooms] =&gt; 1.5 [square_footage] =&gt; 973 [zip_code] =&gt; ** [created] =&gt; 2013-08-13 18:30:34 [modified] =&gt; 2013-08-15 19:08:32 ) [Bill] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 2 [expense_type_id] =&gt; 1 [property_id] =&gt; 2 [frequency] =&gt; monthly [start_date] =&gt; 2013-09-16 [payee] =&gt; ** [amount] =&gt; ** [created] =&gt; 2013-08-20 19:57:41 [modified] =&gt; 2013-08-20 20:57:02 ) [1] =&gt; Array ( [id] =&gt; 4 [expense_type_id] =&gt; 4 [property_id] =&gt; 2 [frequency] =&gt; monthly [start_date] =&gt; 2013-09-15 [payee] =&gt; ** [amount] =&gt; 195 [created] =&gt; 2013-08-20 20:38:11 [modified] =&gt; 2013-08-20 20:38:11 ) ) ) </code></pre> <p>For the life of me, I can not get the array to contain the details of ExpenseType under Bill. Suggestions please!</p> <p>UPDATE: When setting the contain method on the model, Cake now reports:</p> <blockquote> <p>Model "Bill" is not associated with model "ExpenseType"</p> </blockquote> <p><strong>Bill Model</strong></p> <pre><code> class Bill extends AppModel { /******************* * Variables * *******************/ var $actsAs = array('Containable'); /********************* * Model Associations * **********************/ public $belongsTo = array('Property'); public $hasOne = array('ExpenseType'); } </code></pre> <p><strong>ExpenseType Model</strong></p> <pre><code>class ExpenseType extends AppModel { /******************* * Variables * *******************/ var $actsAs = array('Containable'); /******************* * Model Associations * *******************/ public $belongsTo = array('Bill'); } </code></pre> <p><strong>Table Structure</strong></p> <ul> <li>bills <ul> <li>id</li> <li>expense_type_id</li> <li>property_id</li> <li>...</li> </ul></li> <li>expense_types <ul> <li>id</li> <li>name</li> <li>description</li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload