1

我一直很喜欢 CakePHP 的标签插件,并且使用简单的查询获得了很好的结果,例如:

$this->Upload->find('all');

这将返回我所有的上传内容以及相关的标签。精彩的。

但是,如果我尝试通过标签进行搜索:

$this->Upload->find('all', array('conditions' => array('Tag.name' => $tagname)));

它失败了,抱怨它找不到“标签”列。手动搜索失败并出现完全相同的错误:错误:SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'Tag.name'

$tag = $this->Upload->find('all', array('joins' => array(
        array( 
            'table' => 'tagged',
            'alias' => 'Tagged',
            'type'  => 'left',
            'foreignKey' => false,
            'conditions' => array( 'Tagged.foreign_key' => 'Upload.id'),
        array( 
            'table' => 'tags',
            'alias' => 'Tag',
            'type' => 'left',
            'foreignKey' => false, 
            'conditions' => array('Tag.id' => 'Tagged.tag_id')
            )
        ),
        'conditions' => array( 'Tag.name' => $tagname)

    )));    

知道我在这里做错了什么吗?标签插件不是为了做到这一点吗?我想知道如何根据标签对上传进行条件搜索。

为标签插件生成的表:

mysql> show columns from tagged
    -> ;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | varchar(36)  | NO   | PRI | NULL    |       |
| foreign_key  | varchar(36)  | NO   |     | NULL    |       |
| tag_id       | varchar(36)  | NO   |     | NULL    |       |
| model        | varchar(255) | NO   | MUL | NULL    |       |
| language     | varchar(6)   | YES  | MUL | NULL    |       |
| times_tagged | int(11)      | NO   |     | 1       |       |
| created      | datetime     | YES  |     | NULL    |       |
| modified     | datetime     | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

标记表:

mysql> show columns from tags;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | varchar(36) | NO   | PRI | NULL    |       |
| identifier | varchar(30) | YES  | MUL | NULL    |       |
| name       | varchar(30) | NO   |     | NULL    |       |
| keyname    | varchar(30) | NO   |     | NULL    |       |
| weight     | int(2)      | NO   |     | 0       |       |
| created    | datetime    | YES  |     | NULL    |       |
| modified   | datetime    | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4

1 回答 1

0

您也许可以尝试手动设置条件。我不确定标签插件是如何工作的,但如果上传模型与标签有 hasMany 关系,那么这样的事情可能会起作用:

$this->Upload->hasMany['Tag']['conditions']['name'] = $tagname;
$this->Upload->hasMany['Tag']['conditions']['model'] = 'Upload';
$this->Upload->hasMany['Tag']['conditions']['foriegn_id'] = $this->Upload->id;
$this->Upload->find('all');

或者类似的东西$this->Upload->Tag->find('all', array('conditions' => array('Tag.name' => $tagname)));可能会更好(但不确定它是否会起作用)。

于 2013-07-29T18:44:17.753 回答