Unbind无法找到明显存在的模型

时间:2013-07-30 16:33:04

标签: php sql cakephp

我正在尝试对名为uploads的模型进行查询,在那里我搜索具有给定标记的所有上传。我正在使用CakeDC标签插件,我正在使用本教程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 clause'中的未知列'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 

2 个答案:

答案 0 :(得分:0)

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

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

请参阅className的插件名称前缀。对于Tag模型也是如此。 See the book.

答案 1 :(得分:0)

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

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

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