First of all, your join-table must be called authors_papers or
papers_authors, you don't need any model or controller for this table, cake
will detect it automatically.

Put this code in the models who're connected to eachother:
Author:
        var $hasAndBelongsToMany = array(
                'Paper' => array(
                        'className' => 'Paper',
                        'joinTable' => 'authors_papers',
                        'foreignKey' => 'author_id',
                        'associationForeignKey' => 'paper_id',
                        'unique' => true,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => ''
                )
        );

Paper:
        var $hasAndBelongsToMany = array(
                'Author' => array(
                        'className' => 'Author',
                        'joinTable' => 'authors_papers',
                        'foreignKey' => 'paper_id',
                        'associationForeignKey' => 'author_id',
                        'unique' => true,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => ''
                )
        );

This should do the trick.
More info: http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM


Lorenzo Bettini wrote:
> 
> Hi
> 
> I have Paper and Author in HABTM relation (AuthorsPaper is the join 
> table model).
> 
> If I try to perform a find on the Paper model which involves also the 
> authors in the condition I get an error since only the papers table is 
> used for the conditions (even though recursive is set to 1).
> 
> Thus, I made an explicit join:
> 
> $joins[] = array(
> 'table' => $this->AuthorsPaper->tablePrefix.$this->AuthorsPaper->useTable,
> 'alias' => 'AuthorsPaper',
> 'conditions' =>
> array('AuthorsPaper.paper_id = Paper.id AND AuthorsPaper.author_id' => 
> $authors)
> );
> 
> $this->find('all',
> array(
> 'joins' => $joins,
> 'conditions' => $conditions
> )
> );
> 
> where $conditions includes conditions on papers' fields.
> 
> Is this the correct approach?
> 
> In particular, I haven't found any "official" documentation on joins but 
> on some blog posts or on bakery (e.g., 
> http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find).
> 
> I'm asking since using joins requires specifying an explicit table name 
> (which can be achieved anyway using tablePrefix and useTable so that it 
> works also with fixtures in unit tests).
> 
> thanks in advance
>       Lorenzo
> 
> -- 
> Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
> HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
> BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others
> with their CakePHP related questions.
> 
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group
> at http://groups.google.com/group/cake-php?hl=en
> 
> 

-- 
View this message in context: 
http://old.nabble.com/find-in-the-presence-of-HABTM-tp26991562p27026817.html
Sent from the CakePHP mailing list archive at Nabble.com.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to