保存期间CakePHP更新关联记录

时间:2019-10-23 18:19:23

标签: cakephp-3.0 cakephp-3.x

在对User表执行更新之后,我试图更新关联的Student表。

我不使用patchEntity,而是使用isNew()告诉CakePHP是创建还是更新数据;

Student表hasOne的用户为dependent。用户表belongsTo是学生。

我正在尝试通过创建与用户具有一级关联的新实体来更新学生的用户电子邮件。

data => [
    'student_name' => 'John Doe', 
    'user' => [
        'email' => 'PleaseUpdate@email.com'
    ]
]
@Student Table

public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation)
    {
        // Create or Patch Entity
        $existing = $this->find('all')->where([
            'id_number' => $entity->get('id_number'),
            'institution_id' => $entity->get('institution_id')
        ]);

        if ($existing->isEmpty()) {
            // Performs INSERT
            debug("isNew SET True");
            $entity->isNew(true);
        } else {
            // Performs UPDATE
            debug($existing->first());
            $entity->set('id', $existing->first()->id);
            $entity->isNew(false);
        }

学生记录更新。但是,cake为用户插入了一条新记录。我希望Cake更新通过关联已附加到记录的现有用户。

我该怎么做?

谢谢

0 个答案:

没有答案
相关问题