CakePHP 3 - Problem with Nested Eager Loading

0

I have the following table structure:

  • Users
  • Tools
  • Groups

All of them have many to many associations

And I already have the pivot tables:

  • groups_tools
  • groups_users
  • tools_users

I'm trying to do the following query:

TableRegistry::get('Users')
               ->find()
               ->where(['id' => 1])
               ->contain(['Groups.Tools'])
               ->first();

I'm getting the following error

Groups are not associated with Tools

However, I think I'm following all the conventions.

class GroupsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('Tools');
        $this->belongsToMany('Users');
    }
}

class ToolsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('Users');
        $this->belongsToMany('Groups');
    }
}

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('Tools');
        $this->belongsToMany('Groups');
    }
}
    
asked by anonymous 13.07.2015 / 17:16

1 answer

0

I discovered the problem, as I was creating a plugin, the relationships between tables need to contain the namespace of the plugin:

class GroupsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('SeuPlugin.Tools');
        $this->belongsToMany('SeuPlugin.Users');
    }
}

class ToolsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('SeuPlugin.Users');
        $this->belongsToMany('SeuPlugin.Groups');
    }
}

class UsersTable extends Table
{
    public function initialize(array $config)
    {  
        $this->addBehavior('Timestamp');
        $this->belongsToMany('SeuPlugin.Tools');
        $this->belongsToMany('SeuPlugin.Groups');
    }
}
    
14.07.2015 / 15:56