我一直在享受CakePHP的标签插件,我使用简单的查询获得了很好的结果:
$这 - > Upload->发现( '全部');
这会返回我的所有上传内容以及相关标签。精彩。
但是,如果我尝试通过标签搜索:
$ 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 | |
+------------+-------------+------+-----+---------+-------+
答案 0 :(得分:0)
您可以尝试手动设置条件。我不确定Tag插件是如何工作的,但是如果Upload模型与Tags有一个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)));
之类的东西可能会更好(不确定那个是否会起作用)。