在对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更新通过关联已附加到记录的现有用户。
我该怎么做?
谢谢