I'm trying to do a query on a model called uploads, where I search for all uploads that have a given tag. I am using the CakeDC Tags plugin, and I am working from this tutorial, http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations. I am trying to overcome the fact that plugin provides two queries (which cannot be searched) by default.
So, I am attempting to do a join via the bindModel function:
$this->Upload->Behaviors->load('Containable');
$this->Upload->unbindModel(array(
'hasAndBelongsToMany' => array('Tag')
));
$this->Upload->bindModel(array(
'hasOne' => array(
'Tagged' => array(
'className' => 'Tags.Tagged',
'foreignKey' => false,
'conditions' => array('Tagged.foreign_key = Upload.id')
),
'Tag' => array(
'foreignKey' => false,
'conditions' => array('Tag.id = Tagged.tag_id')
)
)
));
$tag = $this->Upload->find('all', array(
'contain' => array('Tag'),
'conditions' => array( 'Tag.name' => $tagname)
));
However: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Tagged.tag_id' in 'on clause'
I believe that this might be because Tagged is just a join table, and not a proper model. How can I get the table to join as I need it to?
The SQL dump: SQL Query:
SELECT
Upload.id,
Upload.name,
Upload.description,
Upload.created,
Upload.modified,
Upload.filetype,
Upload.numberofviews,
Upload.numberofdownloads,
Upload.model_dir,
Upload.model,
Upload.owner,
Upload.license,
Upload.category,
Upload.default_picture,
Upload.soft_delete,
Tag.id,
Tag.identifier,
Tag.name,
Tag.keyname,
Tag.weight,
Tag.created,
Tag.modified
FROM
database.uploads AS Upload
LEFT JOIN
database.tags AS Tag ON (Tag.id = Tagged.tag_id)
WHERE
1 = 1