我正在尝试对名为 uploads 的模型进行查询,在其中搜索具有给定标签的所有上传。我正在使用 CakeDC Tags 插件,并且正在学习本教程http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations。我试图克服插件默认提供两个查询(无法搜索)的事实。
所以,我试图通过 bindModel 函数进行连接:
$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)
));
但是:SQLSTATE [42S22]:找不到列:1054 'on 子句'中的未知列 'Tagged.tag_id'
我相信这可能是因为 Tagged 只是一个连接表,而不是一个合适的模型。我怎样才能让桌子在我需要的时候加入?
SQL 转储: SQL 查询:
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