0
votes

I'm trying to do a query on a model called uploads, where I search for all uploads that have a given tag. I am using the CakeDC Tags plugin, and I am working from this tutorial, http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations. I am trying to overcome the fact that plugin provides two queries (which cannot be searched) by default.

So, I am attempting to do a join via the bindModel function:

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

However: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Tagged.tag_id' in 'on clause'

I believe that this might be because Tagged is just a join table, and not a proper model. How can I get the table to join as I need it to?

The SQL dump: SQL Query:

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

2 Answers

0
votes

It's normal that it can't find the model if you don't tell the association to look for the model in the plugin it belong to. This is not an issue of the plugin but your associations.

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

See the plugin name prefix of the className. Same for the Tag model. See the book.

0
votes

The answer, as related https://github.com/CakeDC/tags/wiki/Find-tagged-objects is to not do a join at all, and let the plugin do the work for me.

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

Sadly, it seems the Uploads associated information is left out of the query. I'm sure I'll find a way around it.