Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I link these models to query multi tables via CakePHP associations?
    text
    copied!<p>I am using cakephp 2.3.2 and I need to do a query on multi tables.</p> <p>I have this database:</p> <pre><code>-------------------------------------------------------------- | Users | Agents | Companies | Ads | -------------------------------------------------------------- | id | id | id | id | | username | name | company | title | | password | lastname | sector | message | | | user_id | user_id | user_id | -------------------------------------------------------------- </code></pre> <p>These are the associations (Models):</p> <p><strong>User</strong></p> <ul> <li>hasOne Agent</li> <li>hasOne Company</li> <li>hasMany Ads</li> </ul> <p><strong>Agent</strong></p> <ul> <li>belongsTo User</li> </ul> <p><strong>Company</strong></p> <ul> <li>belongsTo User</li> </ul> <p><strong>Ad</strong></p> <ul> <li>belongsTo User</li> </ul> <p>(<strong><em>NOTE:</em></strong> <em>Please, keep in mind that when I add a new user that user could be an Agent <strong>OR</strong> a Company</em>.)</p> <p><strong>QUESTION:</strong></p> <p>In my <strong>AdsController</strong> I have an action named <strong>view</strong>, there I read two params that I receve from Route:</p> <pre><code>$this-&gt;params['id'] $this-&gt;params['sector'] </code></pre> <p>I need to do a query to check if the <strong><em>id</em></strong> is really associated to an <strong>Ad.id</strong> and If the <strong><em>sector</em></strong> is associated to <strong>Company.sector</strong></p> <p>I would like to check it with <strong>ONE find('first')</strong> <em><strong>instead of</em></strong> checking</p> <ol> <li>If the ID exists</li> <li>If the sector exists and it is associated to the user_id</li> </ol> <p>How could I do it ?</p> <p><em>(If the query finds Ad.id and Company.sector I need to retrieve all fields of Ad and Company)</em></p> <hr> <p>At the moment my <strong>find('first'</strong>) in AdsController/view is:</p> <pre><code>$options = array( 'fields' =&gt; array( 'Ad.*' ), 'contain' =&gt; array( 'User' =&gt; array( 'Company' =&gt; array( 'fields' =&gt; array( 'Company.*' ), 'conditions' =&gt; array( 'Company.sector' =&gt; $this-&gt;params['sector'], ), ) ) ), 'conditions' =&gt; array( 'Ad.id' =&gt; $this-&gt;params['id'], ) ) $data = $this-&gt;Ad-&gt;find('first', $options); debug($data); </code></pre> <p>The problem is that <strong>Company</strong> is now shown in the result (array).</p> <p>Please, keep in mind that I only need to retrieve the array IF:</p> <ul> <li>The ID of the AD exists</li> <li>The sector of the Company exists</li> </ul> <p>If one of above are not "true" I would like to get an empty array.</p> <p><em>Obviously I have added Containable behavior in Ad model.</em></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