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.

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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样