我一直很喜欢 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 | |
+------------+-------------+------+-----+---------+-------+