HABTM selection seemingly ignores joinTable

Posted by TheCapn on Stack Overflow See other posts from Stack Overflow or by TheCapn
Published on 2012-11-05T22:23:56Z Indexed on 2012/11/05 23:00 UTC
Read the original article Hit count: 166

Filed under:
|

I'm attempting to do a HABTM relationship between a Users table and Groups table. The problem is, that I when I issue this call:

$this->User->Group->find('list');

The query that is issued is:

SELECT [Group].[id] AS [Group__id], [Group].[name] AS [Group__name] FROM [groups] AS [Group] WHERE 1 = 1

I can only assume at this point that I have defined my relationship wrong as I would expect behavior to use the groups_users table that is defined on the database as per convention. My relationships:

class User extends AppModel {
        var $name = 'User';
        //...snip...
    var $hasAndBelongsToMany = array(
        'Group' => array(
            'className'             => 'Group',
            'foreignKey'            => 'user_id',
            'associationForeignKey' => 'group_id',
            'joinTable'             => 'groups_users',
            'unique'                => true,
        )
    );
        //...snip...
}

class Group extends AppModel {
    var $name = 'Group';
    var $hasAndBelongsToMany = array ( 'User' => array(
        'className'             => 'User',
        'foreignKey'            => 'group_id',
        'associationForeignKey' => 'user_id',
        'joinTable'             => 'groups_users',
        'unique'                => true,
    ));
}

Is my understanding of HABTM wrong? How would I implement this Many to Many relationship where I can use CakePHP to query the groups_users table such that a list of groups the currently authenticated user is associated with is returned?

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about habtm