0
votes

I need to build the associations like Group hasMany users and User belongToMany groups.

But I can't get the right result, it always use the wrong table instead groups_relations

My models:

class GroupsTable extends Table
{
    public function initialize(array $config)
    {
        $this->setTable('groups');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->hasMany('Users', [
            'joinTable'     => 'groups_relations',
            'foreignKey'    => 'user_id',
        ]);
    }
}


class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('user_users');
        $this->belongsToMany('Groups', [
            'joinTable'     => 'groups_relations',
            'foreignKey'    => 'group_id',
        ]);

    }
}

class GroupsRelationsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('groups_relations');
        $this->setDisplayField('group_id');
        $this->setPrimaryKey('id');

           $this->belongsTo('Groups', [
                'foreignKey' => 'group_id',
                'joinType'   => 'INNER'
                ]);

            $this->belongsToMany('Users', [
                'foreignKey' => 'user_id',
                'joinType'   => 'INNER'
                ]);

        }
}

And my table groups_relations:

id | group_id | user_id

I run query as:

 $groupsWithUsers = $this->Groups->find('all', array(
        'contain' => array('Users')
        ));

I can't understand how to tell to cake use my intermediary table and append reuslts to array.

2

2 Answers

1
votes

joinTable is not a valid configuration key for a hasMany association. I think that you want to have Groups belongsToMany Users. Another clue about this is that hasMany is the "opposite" of belongsTo, while belongsToMany is it's own opposite. (That is, if A hasMany B, then B belongsTo A, but if A belongsToMany B, then B belongsToMany A.) Note that you will also want to change your GroupsRelations association with Users to belongsTo.

Is this code that was baked for you? Because it should know better. When I run into sticky association problems, I sometimes have Cake bake the model code for me, and then look at how the result differs from what I've written.

0
votes

Rather than trying to use the relation the way you are doing why not just select from the relations table in the first place. This seems like the more Cake way of doing things. You can exclude the conditions clause if you want all data back.

$groupsWithUsers = $this->GroupsRelations->find('all', array(
    'contain' => ['Users', 'Groups'],
    'conditions' => ['Group.id' => $id]
    )
);

After further looking into this I found something I have not used but seems to fit exactly what you need its a belongsToMany using an intermediary table. In your table file for the users you would add the following. A similar entry would be added to the group page.

    $this->belongsToMany('Groups', [
        'through' => 'GroupRelations',
    ]);