Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's possible to use CakePHP's bindModel &amp; unbindModel to build up relations that don't rely on Cake's automagic goodness.</p> <p>Since you were interested in doing the find on the <strong>Product</strong> model, I'll set out an example that can be called from the <strong>Products</strong> controller:</p> <pre><code>// unbind Artist $this-&gt;Product-&gt;unbindModel(array( 'belongsTo' =&gt; array('Artist') )); // bind Artist &amp; Person using explicit conditions $this-&gt;Product-&gt;bindModel(array( 'hasOne' =&gt; array( 'Artist' =&gt; array( 'foreignKey' =&gt; false, 'conditions' =&gt; array('Artist.id = Product.artist_id') ), 'Person' =&gt; array( 'foreignKey' =&gt; false, 'conditions' =&gt; array('Person.id = Artist.person_id') ), ) )); // return the person $person = $this-&gt;Product-&gt;find('first', array( 'conditions' =&gt; array( 'Product.id' =&gt; 1 // or any other condition ), 'fields' =&gt; array( 'Person.first_name', 'Person.last_name' ) )); </code></pre> <p>What's happening here?</p> <p>Firstly, we unbind the <strong>Product</strong> model's relationship with the <strong>Artist</strong> model.</p> <p>Secondly, we bind the <strong>Artist</strong> and <strong>Person</strong> models by explicitly defining the relationships and disabling Cake's automatic forigenKey connections.</p> <p>Lastly, we do a 'first' find on the <strong>Product</strong> model and only request the '<strong>first_name</strong>' &amp; '<strong>last_name</strong>' of the <strong>Person</strong>.</p>
 

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