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条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失