I'm using the following (working) MySQL query to get a count of all the collection_items for a collection_keyword
SELECT ck.id
, ck.working_title
, count(*) as matches
FROM collection_keywords ck
JOIN collection_keywords_collection_items ckci
ON ckci.collection_keyword_id = ck.id
JOIN nl_collection_items nci
ON nci.id = ckci.collection_item_id
GROUP
BY ck.id
SQL Fiddle: http://sqlfiddle.com/#!9/7b4b0a/1
Which returns the following information
+----+---------------+---------+
| id | working_title | matches |
+----+---------------+---------+
| 1 | First Title | 5 |
| 2 | Second Title | 10 |
| 3 | Third Title | 15 |
+----+---------------+---------+
I'm trying to replicate this query using CakePHP 2.9., using the join method but
the result is returning empty
This is my CakePHP code
$collectionKeywords = $this->CollectionKeyword->find('all', array(
'contain' => array(),
'fields' => array('CollectionKeyword.id, CollectionKeyword.working_title', 'count(*) AS matches'),
'joins' => array(
array(
'table' => 'collection_keywords_collection_items',
'type' => 'INNER',
'conditions' => array(
'collection_keywords_collection_items.collection_keyword_id' => 'CollectionKeyword.id'
)
)
),
'group' => 'CollectionKeyword.id',
));
The relations are defined as follows
class CollectionKeyword extends PluginCollectionKeyword
{
public $hasAndBelongsToMany = array(
'CollectionItem' => array(
'className' => 'CollectionItem',
'joinTable' => 'collection_keywords_collection_items',
'foreignKey' => 'collection_keyword_id',
'associationForeignKey' => 'collection_item_id',
)
);
}
And the CollectionItem relation
class CollectionItem extends PluginCollectionItem
{
public $hasAndBelongsToMany = array(
'CollectionKeyword' => array(
'className' => 'CollectionKeyword',
'joinTable' => 'collection_keywords_collection_items',
'foreignKey' => 'collection_item_id',
'associationForeignKey' => 'collection_keyword_id',
),
);
}