Note that there are some explanatory texts on larger screens.

plurals
  1. POJoining to parent table in CakePHP Model->find()
    text
    copied!<p>I have a hierarchy of CakePHP models that looks like:</p> <p><code>Users &gt; Feedfolders &gt; Feeds &gt; Entries</code></p> <p>From within the FeedsController, I'm trying to get a count of all unread Entries based on user ID grouped by feed ID. I'm really at a loss as to how I should be doing this in a way that fits within Cake's way of doing things. I feel like Cake's understanding of the models based on the <code>$belongsTo</code> and <code>$hasMany</code> properties in the models should let it resolve the joins appropriately, but I'm not sure what I'm doing wrong. I've tried several <code>find()</code> variations with no success. Most recently I tried:</p> <pre><code>$result = $this-&gt;Feed-&gt;find('all', array( 'conditions' =&gt; array('User.id' =&gt; $this-&gt;Auth-&gt;user('id'), 'Entry.read' =&gt; 1), 'fields' =&gt; array('count(Entry.feed_id) as Count', 'Entry.feed_id'), 'group' =&gt; 'Entry.feed_id', 'recursive' =&gt; 2) ); </code></pre> <p>Which generates the following SQL:</p> <pre><code>SELECT count(`Entry`.`feed_id`) as Count, `Entry`.`feed_id`, `Feed`.`id` FROM `feeds` AS `Feed` LEFT JOIN `feedfolders` AS `Feedfolder` ON (`Feed`.`feedfolder_id` = `Feedfolder`.`id`) WHERE `User`.`id` = 5 AND `Entry`.`read` = 1 GROUP BY `Entry`.`feed_id` </code></pre> <p>Which results in the following error:</p> <pre><code>SQL Error: 1054: Unknown column 'Entry.feed_id' in 'field list' [CORE/cake/libs/model/datasources/dbo_source.php, line 684] </code></pre> <p>I see the problem with the SQL, and could hand write a query to do what I want, but this seems like the sort of thing that should be doable within CakePHP without resorting to hand-crafted SQL.</p> <p>How should I setup my <code>find()</code> call to do what I want, and, does my plan to find unread Entries via the FeedsController make sense from an MVC standpoint?</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