我正在使用CakeDC用户,作为注册的一部分,我希望注册人选择一些与应用程序中的另一个模型对应的复选框值。我能够很好地获得表单来加载选择,但我无法将其保存到联接表中,即使我已经在所有的关联和可访问性看起来相关的地方添加了这些关联和访问性。该表单的显示方式与其他区域相似,在这些区域,我使用不同的模型保存相同类型的关联。
TypesTable.php
{
public function initialize(array $config): void
{
//$this->addBehavior('Timestamp');
$this->setDisplayField('type');
$this->setPrimaryKey('id');
$this->belongsToMany('Words');
$this->belongsToMany('Users');
$this->belongsTo('Languages', [
'foreignKey' => 'language_id',
'joinType' => 'INNER',
]);
}```
UsersTable.php (in the plugin folders)
```class UsersTable extends Table{
..。
public function initialize(array $config): void{ parent::initialize($config); $this->setTable('users'); $this->setDisplayField('username'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); $this->addBehavior('CakeDC/Users.Register'); $this->addBehavior('CakeDC/Users.Password'); $this->addBehavior('CakeDC/Users.Social'); $this->addBehavior('CakeDC/Users.LinkSocial'); $this->addBehavior('CakeDC/Users.AuthFinder'); $this->hasMany('SocialAccounts', [ 'foreignKey' => 'user_id', 'className' => 'CakeDC/Users.SocialAccounts', ]); $this->hasMany('Types', [ 'foreignKey' => 'user_id', 'targetForeignKey' => 'type_id', 'joinTable' => 'types_users']);}```发布于 2022-03-02 05:52:42
为了使" belongsToMany“关联在此实例中工作,两个*Table.php文件必须具有作为belongsToMany列出的关系。不要忘记在实体中也可以访问字段。
还需要将关联数组放置在patchEntity函数中的RegisterBehavior.php文件中。
UserTable.php (在CakeDC用户中)
public function initialize(array $config): void
{
...
$this->belongsToMany('Types', [
'foreignKey' => 'user_id', 'targetForeignKey' => 'type_id',
'joinTable' => 'types_users']);
}TypesTable.php (应用程序)
class TypesTable extends Table
{
public function initialize(array $config): void
{
....
$this->belongsToMany('Users');
$this->belongsTo('Languages', [
'foreignKey' => 'language_id',
'joinType' => 'INNER',
]);
}RegisterBehavior.php
$user = $this->_table->patchEntity(
$user,
$data,
['validate' => $validator ?: $this->getRegisterValidators($options), 'associated' => ['Types']]
);https://stackoverflow.com/questions/71280151
复制相似问题