duanbanzhi4419 2014-05-21 08:41
浏览 11
已采纳

在cakephp中一次创建多个模型

What I'm trying to reach is to register a User and a Firm at once. So I would need to insert into 3 tables: users, firms, and firms_users. CakePHP should do this automatically, because I've set the $hasAndBelongsToMany associtation in the models. But during the registration, only the users table gets written. Am I missing something?

registration form

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
        <?php 
        echo $this->Form->input('User.email', array('type' => 'email')); //standard HTML5 email validation
        echo $this->Form->input('User.password');
        echo $this->Form->input('Firm.0.name');
        echo $this->Form->input('Firm.0.zipcode');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

the relevant part of User model

public $hasAndBelongsToMany = array(
    'Firm' => array(
        'className' => 'Firm',
        'joinTable' => 'firms_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'firm_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    ),

and the relevant part of Firm model

class Firm extends AppModel {

public $hasAndBelongsToMany = array('User'=>array('className'=>'User'));

finally the UsersController / show_reg_form action

public function show_reg_form(){
        if ($this->request->is('post')) {
            $this->loadModel('Firm');
            $this->User->create();
            $this->Firm->create();
            if ($this->User->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'loggedin','loggedin'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
}
  • 写回答

4条回答 默认 最新

  • dongtaotao19830418 2014-05-21 11:22
    关注

    I think this code will work

    public function show_reg_form(){
            if ($this->request->is('post')) {
                $this->User->create();
                if ($this->User->save($this->request->data)) {
                     $this->Firm->create();
                     if($this->Firm->save($this->request->data))
                     $this->Session->setFlash(__('The user has been saved'));
                     return $this->redirect(array('action' => 'loggedin','loggedin'));
                }
                 else{
                 $this->Session->setFlash(
                    __('The user could not be saved. Please, try again.')
                );
               }
            }
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?