0

我正在尝试对名为 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 
4

2 回答 2

0

如果您不告诉关联在它所属的插件中查找模型,它找不到模型是正常的。这不是插件的问题,而是您的关联问题。

 'hasOne' => array(
            'Tagged' => array(
                'className' => 'Tags.Tagged',
                'foreignKey' => false,

查看 className 的插件名称前缀。Tag 模型也是如此。见书。

于 2013-07-30T17:25:02.957 回答
0

与https://github.com/CakeDC/tags/wiki/Find-tagged-objects相关的答案是根本不进行连接,让插件为我完成工作。

$tag = $this->Upload->Tagged->find('tagged', array('by' => $tagname, 'model' => 'Upload', 'link' => 'User' ));

可悲的是,似乎上传相关信息被排除在查询之外。我相信我会找到解决办法的。

于 2013-07-30T19:17:08.040 回答