In CakePHP 2.4, I'm trying to generate conditions for a search query that searches for data that has been tagged across a HABTM relationship.
My problem is twofold: I can't get my query to return only data tagged with ALL of my search terms, and I can't get my query to return results for partial tags.
This loop generates a working query returning data with ANY of the tags in the search query.
foreach($tags as $tag) {
$conditions['Tag.name'][] = $tag;
}
$query = $this->Tagged->getQuery('all', array(
'conditions' => $conditions,
'fields' => array('foreign_key'),
'contain' => array('Tag')
));
I'd like to get the loop to generate conditions returning only data tagged with ALL the search terms, not data tagged with any of them, and to return matches for partial terms.
EDIT 2:
My database schema looks like this (I'm using the CakeDC Tags plugin to add tags)
Posts
id | other_data
public $hasAndBelongsToMany = array(
'Tag' => array(
'with' => 'Tagged'));
Tagged
id | model | foreign_key | tag_id
Tags
id | name
This is the join I'm trying to use: It complains when I use Tag
as an alias: Not unique table/alias: 'Tag'
.
$joins = array(
array(
'table' => 'tags',
'alias' => 'queryTag',
'type' => 'LEFT',
'conditions' => array(
'queryTag.name' => 'keyword'
)
),
);
$query = $this->Tagged->getQuery('all', array(
//'conditions' => array('Tag.name LIKE' => '%' . $data['tags'] . '%'),
'joins' => $joins,
'fields' => array('foreign_key'),
'contain' => array('Tag')
));
return $query;