保存与 CakeDC 用户的关联

发布于 2025-01-10 05:49:52 字数 1572 浏览 0 评论 0原文

我正在使用 CakeDC 用户,作为注册的一部分,我希望注册者选择一些与应用程序中的另一个模型相对应的复选框值。我能够很好地获取表单来加载选项,但无法将其保存到连接表中,即使我已经添加了所有看起来相关的关联和可访问性。该表单的显示方式与其他区域类似,我在其他区域中保存与不同模型的相同类型的关联。

类型表.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']);
    }```

I am using CakeDC Users and as part of the registration I would like the registrant to choose some checkbox values that correspond to another Model in the application. I am able to get the form to load the choices just fine, but I am unable to get it to save to the join table even though I have added all of the associations and accessibilities where it would seem relevant. The form is displaying in a similar way to other areas where I am saving the same type of association with a different model.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2025-01-17 05:49:52

为了使“belongsToMany”关联在此实例中发挥作用,两个 *Table.php 文件都必须将关系列为“belongsToMany”。不要忘记在实体中也使该字段可访问。

还需要将关联的数组放置在 RegisterBehavior.php 文件的 patchEntity 函数中。

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

In order for a "belongsToMany" association to work in this instance, both *Table.php files must have the relations listed as belongsToMany. Don't forget to make the field accessible in the Entities as well.

One also needs to place the associated array in the patchEntity function in the RegisterBehavior.php file.

UserTable.php (in CakeDC users)

public function initialize(array $config): void
    {
        ...
        $this->belongsToMany('Types', [
            'foreignKey' => 'user_id', 'targetForeignKey' => 'type_id',
        'joinTable' => 'types_users']);
    }

TypesTable.php (in app)

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']]
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文