dongliao1949 2015-12-14 02:09
浏览 20
已采纳

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

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.

Anybody can help? --Here is my code---

///**
 * 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() ?>
  • 写回答

1条回答 默认 最新

  • duanhong1985 2015-12-14 05:29
    关注

    I think you may take the Bookmarker Tutorial as a reference because in the tutorial, new Tags are created while new Bookmarks is created. You can consider this as an idea of your UserInfo creation.

    Well I am new to CakePHP too but have this idea for your case.

    In your controller, controller\UserController, the action add() creates User entity:

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

    In your model, model\Table\UserTable.php, create a function called afterSave() which creates UserInfo after User entity is saved

    $this->UserInfo->newEntity();
    

    So inside newEntity(), you actually set some data which User doesn't have and UserInfo has, besides user_id (user_id should be set if you have already set up the associations of them)

    I strongly recommend to follow all basic tutorials.

    Remarks: I suggest you define model's name clearly as CakePHP conventions should be an important topic for CakePHP's developer. So for the above codes/filenames I typed, could be wrong if they don't match your case exactly. e.g. Users / Users / UsersInfo / UserInfo etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源