在CakePHP中同时保存manys用户及其配置文件

时间:2014-02-13 21:25:27

标签: cakephp cakephp-2.3

如何保存多个用户及其配置文件的数组格式?

有两种型号,用户和个人资料。在配置文件表上,我有外键“user_id”。我尝试使用saveAll命令,但只保存用户并且配置文件表为空。

Out of for(设置数组):

$data = array();

里面的:

$data[] = array(
    'User' => array(
        'username' => $username,
        'password' => $password
    ),
    'Profile' => array(
        'name' => $name,
    )
);

保存命令:

$this->User->saveAll($data)

模型关联:

User hasMany Profile
Profile belongsTo User

Ps。:saveAll可以节省10个用户,但不能保存他们的个人资料; /

4 个答案:

答案 0 :(得分:1)

使用save associated

$this->User->saveAssociated($data);

答案 1 :(得分:0)

我解决了我的问题。

这个问题有两个解决方案。

首先:

此代码:

User hasMany Profile
必须将

更改为hasOne:

User hasOne Profile

第二

如果您有hasMany关系,则需要使用Profile上的数字键创建不同的数组,即使您只添加一个配置文件(也许您将在其他时间添加其他配置文件)。像这样:

$data[] = array(
    'User' => array(
        'username' => $username,
        'password' => $password
    ),
    'Profile' => array(
        0 => array(           //NEW LINE ADDED!!!!
            'name' => $name,
        )                     //NEW LINE ADDED!!!!

    )
);

抱歉浪费你的时间。这个问题将继续存在,只是为了帮助另一个有同样问题的人。

答案 2 :(得分:0)

这是我的应用程序中的代码:

控制器代码:

public function add() {


        if ($this->request->is('post')) {
            $this->Member->create();

            if ($this->Member->User->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The member has been saved.'));
                return $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The member could not be saved. Please, try again.'), 'flash_message_error');
            }
        }
                $countries = $this->Member->Country->fetchList();

        $title_for_layout = 'Membership Signup';
        $this->set(compact('title_for_layout', 'countries'));
    }

用户型号代码:

  public $hasOne = 'Member';

会员型号代码:

 public $belongsTo = array(        

          'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

查看代码:

<div class="row">
     <?php echo $this->Form->create('Member', array('url'=>array('controller'=>'members', 'action'=>'add', 'admin'=>false))); ?>

        <div class="col-md-12 col-sm-12 col-xs-12">
         <h3 class="sign-head">Personal Information</h3>
        </div>

                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('User.username', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'username')); ?>
                  </div>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('User.password', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'Password')); ?>
                  </div>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('Member.first_name', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'Name')); ?>
                  </div>

                  <div class="col-md-12 col-sm-6 col-xs-12">
                     <?php echo $this->Form->submit('submit', array('div'=>FALSE, 'class'=>' submit-contact')); ?>
                  </div>

          <?php echo $this->Form->end(); ?>
                </div>

希望对其他用户有所帮助。

答案 3 :(得分:-1)

  $user = array(
        'username' => $username,
        'password' => $password

     );
     $this->User->save($user);

  $profile = array(
        'name' => $name);
     $this->Profile->save($profile);
  

您必须创建个人资料模型

相关问题