2013-10-13 78 views
0

我,每當我想創建一個用戶我也想創造我的個人資料,有一個belongsTo關係附帶ProfileCakePHP的數據保存到產生額外的模型

現在User模型。

爲此,我創建了以下形式:

<div class="span9" id="lejer"> 
    <?php echo $this->Form->create('User', array('class' => 'form-horizontal')); ?> 
    <?php echo $this->Form->input('group_id', array('type' => 'hidden', 'value' => 3));?> 
    <h3 class="heading3">Dine Personlige Informationer:</h3> 

    <div class="registerbox"> 
     <fieldset> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Fornavn: 
       </label> 

       <div class="controls"> 
        <?php echo$this->Form->input('Fornavn', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Efternavn: 
       </label> 

       <div class="controls"> 
        <?php echo$this->Form->input('Efternavn', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Telefon: 
       </label> 

       <div class="controls"> 
        <?php echo$this->Form->input('Telefon', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        E-mail: 
       </label> 
       <div class="controls"> 
        <?php echo$this->Form->input('E-mail', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 

     </fieldset> 

    </div> 

    <h3 class="heading3">Your Address</h3> 
    <div class="registerbox"> 
     <fieldset> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Addresse: 
       </label> 
       <div class="controls"> 
        <?php echo$this->Form->input('Addresse', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 

      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Postnr: 
       </label> 
       <div class="controls"> 
        <?php echo$this->Form->input('Postnr', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        By: 
       </label> 
       <div class="controls"> 
        <?php echo$this->Form->input('By', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
      <div class="control-group"> 
       <label class="control-label"> 
        <span class="red">*</span> 
        Område: 
       </label> 
       <div class="controls"> 
        <?php echo$this->Form->input('Område', array('class'=> 'input-xlarge', 'label' => '')); ?> 
       </div> 
      </div> 
     </fieldset> 

    </div> 

現在,當我調試,我可以看到,所有的數據都內的下列array

$this->request->data['User']; 
在我UsersController我

現在有以下create動作:

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

但是當我運行這個只有一個User創建不是一個Profile

誰能告訴我我做錯了什麼?我認爲saveAll我會保存到它的關係模型。

+0

這裏閱讀http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related -model-數據hasone-的hasMany-屬於關聯 – lukasz

回答

1

。在你的代碼 需要用戶型號名稱與字段名稱用點分隔像

$這個 - >形式 - >輸入(「User.email」)中的問題; $ sthis-> Form-> input('Profile.Telefon');

之後,你可以在你的控制器下面的代碼保存記錄。

$ this-> Model-> saveAssociated($ this-> request-> data);

更多細節,請閱讀此鏈接: -

> http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array