CakePHP Alias ​​Breaks HABTM Model

Consider the following HABTM relationship in CakePHP 2.2.3:

class User extends AppModel { public $hasAndBelongsToMany = array( 'Role' => array( 'className' => 'Role', 'joinTable' => 'roles_users', 'foreignKey' => 'user_id', 'associationForeignKey' => 'role_id', ) ); } 

This works fine, but when using an alias of type VeryUniqueAlias instead of Role and, accordingly, changing the UserController, the m: n relation is not stored in the database (the data passed to save() in the controller is equivalent).

This does not work:

 class User extends AppModel { public $hasAndBelongsToMany = array( 'VeryUniqueAlias' => array( 'className' => 'Role', 'joinTable' => 'roles_users', 'foreignKey' => 'user_id', 'associationForeignKey' => 'role_id', ) ); } 

This is inconvenient as CakePHP docs states that it should work. Any idea why it doesn't work for me? Did I miss something?

+4
source share
1 answer

Use the c key to specify the model name for the connection table. In your case:

 public $hasAndBelongsToMany = array( 'VeryUniqueAlias' => array( 'className' => 'Role', 'joinTable' => 'roles_users', 'with' => 'RolesUser', // first model pluralized 'foreignKey' => 'user_id', 'associationForeignKey' => 'role_id', ) ); 
0
source

Source: https://habr.com/ru/post/1447862/


All Articles