PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

[FIXED] You can't have an association of the same name with a different target "className" option anywhere in your app

 January 05, 2022     cakephp, cakephp-3.0, permissions, php     No comments   

Issue

I am using slince/cakephp-permission package for adding permissions and roles to my CakePHP application.

I have managed to set it up but when I try to give a permission to a role I see this error:

Slince\CakePermission\Model\Table\RolesTable association "Permissions" of type "manyToMany" to "App\Model\Table\PermissionsTable" doesn't match the expected class "Slince\CakePermission\Model\Table\PermissionsTable". You can't have an association of the same name with a different target "className" option anywhere in your app.

The code that fire this error is the one below

$roleObj = Role::find($role->name);
$roleObj->givePermission($addPermission);

if I var_dump the $roleObj this is the result I obtain

object(Slince\CakePermission\Model\Entity\Role)#239 (13) {
  ["id"]=>
  int(6)
  ["name"]=>
  string(5) "Admin"
  ["slug"]=>
  string(5) "Admin"
  ["created"]=>
  object(Cake\I18n\Time)#258 (3) {
    ["time"]=>
    string(25) "2018-08-22T11:41:28+00:00"
    ["timezone"]=>
    string(3) "UTC"
    ["fixedNowTime"]=>
    bool(false)
  }
  ["modified"]=>
  string(15) "22/08/18, 11:41"
  ["[new]"]=>
  bool(false)
  ["[accessible]"]=>
  array(2) {
    ["id"]=>
    bool(false)
    ["*"]=>
    bool(true)
  }
  ["[dirty]"]=>
  array(0) {
  }
  ["[original]"]=>
  array(0) {
  }
  ["[virtual]"]=>
  array(0) {
  }
  ["[errors]"]=>
  array(0) {
  }
  ["[invalid]"]=>
  array(0) {
  }
  ["[repository]"]=>
  string(16) "_PermissionRoles"
}

These are the relationship in the tables pages

$this->belongsToMany('Permissions', [
            'targetTable' => 'Slince\CakePermission\Model\Table\Permissions',
            'className' => 'Slince\CakePermission\Model\Table\Permissions',
            'foreignKey' => 'role_id',
            'targetForeignKey' => 'permission_id',
            'joinTable' => 'roles_permissions',
            'saveStrategy' => 'append'
 ]);
$this->belongsToMany('Roles', [
            'targetTable' => 'Slince\CakePermission\Model\Table\Roles',
            'className' => 'Slince\CakePermission\Model\Table\Roles',
            'foreignKey' => 'permission_id',
            'targetForeignKey' => 'role_id',
            'joinTable' => 'roles_permissions',
            'saveStrategy' => 'append'
]);

I have tried several different setting but I always get the same error. I think I am missing something when setting the relations between the table up.

Do you guys have any idea how to solve this problem?


Solution

It happens when you have two (or more) associations with name "Permissions" defined within your app and each refers different model class.

You sholuld check if:

  1. Any user defined associantion with name "Permissions" exists. And if so - rename it.
  2. Any other plugin uses "Permissions" association (maybe legacy permissions manager) - then one must be removed or overwritten.


Answered By - r34
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing