Cakephp 3 - Problem with Nested Eager Loading

Asked

Viewed 24 times

0

I have the following table structure:

  • Users
  • Tools
  • Groups

All of them have membership from Many to Many

And I already own the pivot Tables:

  • groups_tools
  • groups_users
  • tools_users

I am trying to do the following query:

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

I’m getting the following error

Groups is not Associated with Tools

However, I believe 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');
    }
}

1 answer

0

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

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');
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.