Note that there are some explanatory texts on larger screens.

plurals
  1. POhasMany through not working the way I want it, though it should
    text
    copied!<p>I've been trying to learn associations and specifically hasMany through because, according to cakephp docs, that's what will be best for my needs.</p> <p>I have three tables</p> <pre><code>drugs id | drug 5 | reality records id | user_id | a-couple-misc-fields 1 | 1 | record_drugs id | drug_id | record_id 1 | 5 | 1 </code></pre> <p>I also have 3 models</p> <pre><code>//Drug.php public $hasMany = array('RecordDrug'); //Record.php public $hasMany = array('RecordDrug'); //RecordDrug.php public $belongsTo = array('Record', 'Drug'); </code></pre> <p>Before I had this, I only had one table '<strong>records</strong>' which, instead of '<strong>drug_id</strong>' simply had '<strong>drug</strong>' which would be '<strong>reality</strong>'. That method obviously worked, but I know that method wouldn't work in the long run.</p> <p>My old query</p> <pre><code>$records=$this-&gt;Record-&gt;find('list', array('fields' =&gt; array('drug', 'sum'), 'group' =&gt; 'drug', 'conditions' =&gt; array('user_id' =&gt; $this-&gt;Auth-&gt;user('id')))); </code></pre> <p>If I were to run this exact same query and use '<strong>drug_id</strong>' instead of '<strong>drug</strong>' I would simply get an array that would be something like this:</p> <pre><code>'5' =&gt; '1' // The Drug =&gt; The Count of the Drug </code></pre> <p>But I need it to display like this:</p> <pre><code>'Reality' =&gt; '1' </code></pre> <p>I understand if this problem is to complex for help, but if anyone has any resources or links I can look up that will help me with this, that would be amazing! Googling only results in showing me how to set up the Join Model but not how to actually use it and retrieve data.</p> <p>EDIT: </p> <pre><code> $this-&gt;loadModel('RecordDrug'); $this-&gt;RecordDrug-&gt;virtualFields['sum'] ='COUNT(*)'; $records=$this-&gt;RecordDrug-&gt;find('list', array('fields' =&gt; array('drug_id', 'sum'), 'group' =&gt; 'drug_id' )); $this-&gt;set('output',$records); debug($records); </code></pre> <p>This mostly gives me what I want;</p> <pre><code>array( (int) 1 =&gt; '1', (int) 2 =&gt; '2' ) </code></pre> <p>The only thing I need to do now is figure out how to change the first value to the actual 'drug' associated with the id.</p> <p><strong>ANSWER</strong></p> <p>So leaving off with me trying to associate the drug_id to the actual drug, this is how I made it display correctly for me:</p> <pre><code> $this-&gt;RecordDrug-&gt;virtualFields['sum'] ='COUNT(*)'; $records=$this-&gt;RecordDrug-&gt;find('list', array('fields' =&gt; array( 'Drug.drug', 'sum'), 'contain' =&gt; array('Drug', 'Record'), //Record is only here if you need the condition, you can remove record complete and the condition completely and it'll still work, but it'll give you ALL the results rather then just yours 'group' =&gt; 'Drug.Drug', 'conditions' =&gt; array('Record.user_id' =&gt; $this-&gt;Auth-&gt;user('id')) )); </code></pre>
 

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