2017-04-21 50 views
0

誰將數據保存在同一用戶表的多個表中有很多關係 如果我更改$ this-> CompanyContact-> saveAll($ this-> request->數據)CompanyContact到用戶則只有用戶的數據保存,如果我改變CompanyContact到公司地址則只有地址保存如何在CakePHP 2.6中爲同一用戶保存多個表中的數據

this my contoller function 

public function editcompanyprofile($id = null) { 
    $this->layout = 'main'; 

    if ($this->request->is(array('put', 'post'))) { 

     $this->request->data['CompanyAddress'] = $this->request->data['Company']['CompanyAddress']; 
     unset($this->request->data['Company']['CompanyAddress']); 

     if ($this->request->data['CompanyProfile']['profile_img']['name'] != "") { 
      $sFileName = time() . "_" . str_replace(" ", "_", $this->request->data['CompanyProfile']['profile_img']['name']); 
      $sPath = "profile"; 
      $file = $this->Pk->uploadImage($this->request->data['CompanyProfile']['profile_img'], $sFileName, $sPath); 

      if ($file['status'] == 'success') { 
       unset($this->request->data['CompanyProfile']['profile_img']); 
       $this->request->data['CompanyProfile']['profile_img'] = $file['url']; 
      } else { 
       $this->request->data['CompanyProfile']['profile_img'] = ""; 
      } 
     } else { 
      unset($this->request->data['CompanyProfile']['profile_img']); 
     } 
     if ($this->CompanyContact->saveAll($this->request->data)) { 

      return $this->redirect(array('controller' => 'users', 'action' => 'companyprofile', $id)); 
     } else { 
      $this->Flash->error(__('The user could not be saved. Please, try again.')); 
     } 
    } else { 

     $options = array('conditions' => array('CompanyContact.company_id'), 'recursive' => 2); 
     $this->request->data = $this->CompanyContact->find('first', $options); 
      $this->set('options', $options); 
     // print_r($this->request->data);exit; 
    } 
} 





<?php echo $this->Form->create('CompanyProfile', array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')); ?> 
<?php echo $this->Form->input('User.id'); ?> 


      <lable id="view-edit-lable">First Name</lable> 
      <?php echo $this->Form->input('User.fname', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?> 
      <br> 
      <lable id="view-edit-lable">Last Name</lable> 
      <?php echo $this->Form->input('User.lname', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?> 
      <br> 
      <lable id="view-edit-lable">Company Name</lable> 
      <?php echo $this->Form->input('Company.company_name', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?> 
      <?php echo $this->Form->input('Company.id', array('type' => 'hidden')); ?> 
      <br> 

      <lable id="view-edit-lable">Website Url</lable> 
      <input type="email" class="form-control" id="Last_name" placeholder="Website" > 
      <br> 
      <lable id="view-edit-lable">Email Address</lable>  
      <?php echo $this->Form->input('User.email', array('class' => 'form-control', 'placeholder' => 'Enter Last Name Here..', 'label' => false, 'required')); ?> 

      <br> 
      <lable id="view-edit-lable">Contact no</lable> 
      <div class="phone-list"> 
       <div class="input-group phone-input" style="margin-bottom: 10px;"> 
        <span class="input-group-btn"> 

         <button type="submit" style="height:45px;font-size: 15px;" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-earphone" data-toggle="dropdown" aria-expanded="false"></span></button> 
        </span> 
        <?php echo $this->Form->input('User.mobile_number', array('class' => 'form-control', 'placeholder' => '+1 (999) 999 9999', 'label' => false)); ?> 

       </div> 

      </div> 
      <br> 

      <lable id="view-edit-lable">Address</lable> 
      <?php echo $this->Form->input('Company.CompanyAddress.address1', array('class' => 'form-control', 'label' => false)); ?> 
      <?php echo $this->Form->input('Company.CompanyAddress.id', array('type' => 'hidden')); ?> 
      <br> 
      <lable id="view-edit-lable">Address 2</lable> 
      <?php echo $this->Form->input('Company.CompanyAddress.address2', array('class' => 'form-control', 'label' => false, 'placeholder' => 'Floor #')); ?> 
      <br> 

      <lable id="view-edit-lable">City</lable> 
      <?php echo $this->Form->input('Company.CompanyAddress.city', array('class' => 'form-control', 'label' => false)); ?> 
      <br> 
      <lable id="view-edit-lable">State</lable> 
      <?php echo $this->Form->input('Company.CompanyAddress.state', array('type' => 'select', 'options' => array('New York' => 'New York', 'Connecticut' => 'Connecticut', 'New Jersey' => 'New Jersey'), 'selected' => 'New York', 'label' => false, 'empty' => 'Select State', 'style' => 'width:100%;', 
       'class' => 'btn dropdown-toggle selectpicker btn-default')); 
      ?> 
      <br> 
      <lable id="view-edit-lable">Zip Code</lable> 
<?php echo $this->Form->input('Company.CompanyAddress.zipcode', array('type' => 'number', 'class' => 'form-control', 'label' => false)); ?> 
       <br> 
       <lable id="view-edit-lable">Country</lable> 
       <?php echo $this->Form->input('Company.CompanyAddress.country', array('type' => 'select', 'options' => array('USA' => 'USA'), 'selected' => 'USA', 'label' => false, 'empty' => 'Select State', 'style' => 'width:100%;', 
        'class' => 'btn dropdown-toggle selectpicker btn-default')); 
       ?> 


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

回答

0

試圖將這段代碼

if ($this->CompanyContact->saveAll($this->request->data)) { 

這樣:

if ($this->CompanyContact->saveAll($this->request->data) && $this->CompanyAddress->saveAll($this->request->data)) { 
+0

其工作,但我也必須存儲在一個多表公司簡介數據 –

0

您可以嘗試像下面那不是你的解決方案,因爲你的代碼是搞砸所以對於特定的模型,然後箱子陣列保存。也別忘了導入模型。

$addrs = $this->request->data['Company']['CompanyAddress']; 
    $data = array('id' => 1, 'address' => $addrs); 
    $this->CompanyAddress->save($data) 
相關問題