gpt4 book ai didi

php - cakephp 3.x 中为不同模型在一页上创建多个表单

转载 作者:行者123 更新时间:2023-11-29 11:49:59 25 4
gpt4 key购买 nike

I have 2 models: User and UserInfo with relation 1-1 (One user have one userinfo).

User(id) is primary key for User and UserInfo(user_id) is both foreign key and primary key for UserInfo.

2 models have the same attribute: email, password.

I want to insert 'user_id', 'email', 'password' to UserInfo when add new User.

But it seems can insert to UserInfo although User is successful saved.

I think it stop when ($this->User->UserInfos->save($userinfo)) run.

有人可以帮忙吗?--这是我的代码---

///**
* Add method
*
* @return void Redirects on successful add, renders view otherwise.
*/
public function add() {
$user = $this->Users->newEntity();
$userinfo = $this->Users->UserInfos->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
$userinfo = $this->Users->UserInfos->patchEntity($userinfo, [
'user_id' => $user['User']['id'],
'email' => $user['User']['email'],
'password' => $user['User']['password'],
]);
if ($this->User->UserInfos->save($userinfo)) {
$this->Flash->success(__('The userinfo has been saved.'));
}
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user', 'userinfo'));
$this->set('_serialize', ['user', 'userinfo']);
}


//Code in add.php
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?php
echo $this->Form->radio('user_type',
[
['value' => '0', 'text' => 'Personal'],
['value' => '1', 'text' => 'Company'],
]);
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->hidden('status', ['value' => '0']);
echo $this->Form->hidden('authority', ['value' => '0']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

最佳答案

我想你可以选择Bookmarker Tutorial作为引用,因为在本教程中,会在创建新的书签 的同时创建新的标签。您可以将此视为您创建 UserInfo 的一个想法。

嗯,我也是 CakePHP 的新手,但对你的情况有这个想法。

在 Controller (controller\UserController)中,操作 add() 创建 User 实体:

public function add() {
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->User->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']); // same controller's index action, or you can set others
}
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}

在您的模型 model\Table\UserTable.php 中,创建一个名为 afterSave() 的函数,该函数创建 UserInfo after User entity is saved

$this->UserInfo->newEntity();

所以在newEntity()中,除了user_id之外,你实际上设置了一些User没有而UserInfo有的数据(如果你已经设置了其中的associations,则应该设置user_id )

我强烈建议遵循所有基本教程。

备注:我建议你明确定义模型名称为CakePHP conventions对于 CakePHP 的开发者来说应该是一个重要的话题。因此,对于我输入的上述代码/文件名,如果它们与您的大小写不完全匹配,则可能是错误的。例如用户/用户/用户信息/用户信息等

关于php - cakephp 3.x 中为不同模型在一页上创建多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com