1
votes

I've been enjoying CakePHP's tag's plugin, and I have been getting great results using simple queries like:

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

This returns all of my uploads, and the associated tags. Wonderful.

However, if I were to try and search via tag:

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

It fails, complaining that it cannot find a "Tag" column. Manual searches fail with the exact same error: Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Tag.name' in 'where clause'

$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)

    )));    

Any idea what I'm doing wrong here? Is the tags plugin not meant to do this? I would like to know how to do a conditional search for Uploads based on tags.

The table generated for the tags plugin:

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    |       |
+--------------+--------------+------+-----+---------+-------+

tag table:

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    |       |
+------------+-------------+------+-----+---------+-------+
1
Can you show us your table? - Perry

1 Answers

0
votes

You could perhaps try to set the conditions manually. I'm not sure how the Tag plugin works, but if the Upload model has a hasMany relation to the Tags, something like this would probably work:

$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');

Or something like $this->Upload->Tag->find('all', array('conditions' => array('Tag.name' => $tagname))); might be better (not sure if that one will work though).