CakePHP 3-使用依赖于关联的关联来保存数据 [英] CakePHP 3 - Saving data with associations where dependant on association

查看:49
本文介绍了CakePHP 3-使用依赖于关联的关联来保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我简化了表格以了解我的核心需求。

For simplicity sake, I have simplified my tables to get my core need across.

我有两个表格:admins和users。管理员表简单地保存了某些与问题不完全相关的信息。

I have two tables: admins and users. The admin table simple holds certain information that is not entirely relevant to the question.

管理员:

+----+---------+
| id | user_id |
+----+---------+
| 1  | 20      |
+----+---------+
| 2  | 25      |
+----+---------+
| 3  | 27      |
+----+---------+

用户:

+----+--------+-----------+
| id | name   | surname   |
+----+--------+-----------+
| 20 | Name 1 | Surname 1 |
+----+--------+-----------+
| 25 | Name 2 | Surname 2 |
+----+--------+-----------+
| 27 | Name 3 | Surname 3 |
+----+--------+-----------+

这个想法是,当超级管理员保存新用户时,填写表格即进行各种形式的用户注册,保存后将保存用户并使用以下命令创建管理员记录新创建的用户ID。

The idea is that when a super admin saves a new user, the fill in a form, being a user registration for of sorts and upon saving it saves the user and creates the admin record with the newly created user id.

显然,如果没有用户帐户,则无法创建管理记录,因为它需要用户ID。 Cake simple返回错误消息,提示需要用户名。

Obviously the admin record cannot be created without the user account existing as it requires the user id. Cake simple returns an error saying "user_id is required".

这是我的表单:

<?= $this->Form->create($user) ?>
<fieldset>
    <legend><?= __('Add Admin') ?></legend>
    <?php
        echo $this->Form->input('user.name');
        echo $this->Form->input('user.surname');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

我尝试用管理员关联创建用户,反之亦然:

I have tried creating a user with an admin association and the other way around:

    $admin = $this->Admins->newEntity();

    if($this->request->is('post')) {
        $this->Admins->patchEntity($admin, $this->request->data,[
            'associated' => ['Users']
        ]);
        $savedAdmin = $this->Admins->save($admin);
    }

    $user = $this->Admins->Users->newEntity();

    if($this->request->is('post')) {
        $this->Admins->Users->patchEntity($user, $this->request->data,[
            'associated' => ['Admins']
        ]);
        $savedUser = $this->Admins->Users->save($user);
    }

我不保存其他实体,因为我知道可以保存关联数据,我只是不是真的如何保存管理记录,因为它取决于用户记录。

I do not what to save separate entities because I know it is possible to saved associated data, I'm just not really how to saved the admin record as it relies on the user record.

推荐答案

首先,创建您的管理实体,然后关联用户(因为admin需要用户ID)。完成后,创建新的用户实体并将其分配给变量。

First, create your admin entity and then associate users (as admin requires the user id). Once you have done that create your new user entity and assign it to a variable.

然后,手动为新创建的用户分配外键,Cake会找出来需要根据您的模型为其分配主要对象。最后,保存您的管理员,它将自动保存您的关联实体。

The, manually assign the foreign key the newly created user, and Cake will figure out it needs to assign it the primary based on your model. Finally, save your admin, and it will automatically save your associated entity.

这就是为什么定义您的所属并在相关模型中有一个很重要的原因,如

That's why it is important to define your belongs to and has one in the relevant models as mentioned in the previous answer.

if ($this->request->is('post')) {
    $admin = $this->Admins->newEntity($this->request->data, [
        'associated' => ['Users']
    ]);

    $user = $this->Admins->Users->newEntity();
    $admin->user_id = [$user];

    $saved = $this->Admins->save($admin);
}

这篇关于CakePHP 3-使用依赖于关联的关联来保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆