【问题标题】:Saving manys Users and their Profiles at the same time in CakePHP在 CakePHP 中同时保存多个用户及其个人资料
【发布时间】:2014-02-13 21:25:27
【问题描述】:

保存多个用户及其个人资料的数组格式应该如何?

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

out of for(设置数组):

$data = array();

for的内部:

$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 个用户,但不保存他们的个人资料;/

【问题讨论】:

  • 你能贴出你试过的代码吗?这将帮助我们
  • 您是否将模型链接/关联在一起?
  • Posted.....saveAll 保存 10 个用户,但不保存他们的个人资料;/

标签: cakephp cakephp-2.3


【解决方案1】:

使用save associated

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

【讨论】:

    【解决方案2】:

    我解决了我的onw问题。

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

    第一:

    这段代码:

    User hasMany Profile
    

    必须改为hasOne:

    User hasOne Profile
    

    第二:

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

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

    很抱歉浪费了您的时间。这个问题会一直存在,只是为了帮助另一个有同样问题的人。

    【讨论】:

      【解决方案3】:

      这是我的应用程序的工作代码:

      控制器代码:

      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>
      

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

      【讨论】:

        【解决方案4】:
          $user = array(
                'username' => $username,
                'password' => $password
        
             );
             $this->User->save($user);
        
          $profile = array(
                'name' => $name);
             $this->Profile->save($profile);
        

        你必须创建一个配置文件模型

        【讨论】:

        • 但我想保存在交易中。如果一个失败,所有失败。而且这个解决方案不关联 user_id。
        • 或者您没有创建配置文件控制器或模型,您已经获得了用户的信息。编写一个 membre_index 函数并使用 users 表中的信息...
        • CakePHP 支持事务但你必须使用明确的见book.cakephp.org/2.0/en/models/transactions.html
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-20
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多