2012-08-07 110 views
3

我有一個函數公司簡介有關係HABTM給用戶控制器,聯合表companies_users我的功能的HABTM數據保存問題

ublic function company_profile(){ 
//$logo= $this->Upload->upload('/img/company_logo', $this->data['logo']['a'],null, array('image/jpeg', 'image/jpg', 'image/png')); 
$log = $this->Auth->User('id'); 
    // retrieve the data of the currently logged in user 
$user = $this->User->find('first',array(
      'conditions'=>array('User.id'=>$log), 
      'recursive'=>1 
)); 
    //pr($user);exit(); 
    if($this->request->is('post') || $this->request->is('put')){ 
     //pr($this->request->data);exit; 
     //pr($this->data);exit(); 
     if(isset($user['Company']) && !empty($user['Company'])){ 
     $newcompany = array(); 
     $this->User->Company->id = $user['Company'][0]['id']; 
     $newcompany['Company'] = $this->data['Company'][0]; 
     $newcompany['Company']['id'] = $user['Company'][0]['id']; 
     //pr($newcompany);exit(); 
     $this->User->Company->save($newcompany); 
     }else{ 
     $this->User->Company->create(); 
     $newcompany = array(); 
     $newcompany['Company'] = $this->data['Company'][0]; 
     $newcompany['Company']['user_id'] = $log; 
     pr($newcompany);exit(); 

     //newlogo = $this->request->data; 
     //$newlogo['Company']['logo'] = $logo['urls']['0']; 
     $this->User->Company->save($newcompany); 
     //pr($newcompany);exit(); 
     $this->Session->setFlash('Company Profile Saved Successfully.'); 
     $this->redirect('/users/dashboard'); 

     } 



    }else{ 
    $this->request->data = $this->User->read(null, $log); 
    } 
$this->loadModel('Category'); 
$categories = $this->Category->find('list'); 
$this->set(compact('user','categories')); 

} 

它爲什麼不保存在接合表?請幫我怎麼會保存在接合表目前我有這個數組

陣列 ( [公司] =>陣列 ( [ID] => [CATEGORY_ID] => 1 [名] => Infoperks解 [描述] => Web開發 [USER_ID] => 1 )

+0

Array([Company] => Array([id] => [category_id] => 1 [name] => Infoperks Solutions [description] => Web development [user_id] => 1) id是否丟失或你只是輸入錯誤? – 2012-08-07 07:31:14

+0

是啊,它沒有身份證,因爲我保存之前我PR它 – 2012-08-07 07:39:30

回答

1

使用'deep' => true可以節省連接表數據提供您所指定的模式升協會。您可以嘗試使用下面的語法來保存連接的數據:

$this->User->save($newcompany, array('deep' => true)); 
//instead of 
//$this->User->Company->save($newcompany); 

This link一定會幫助您保存HABTM相關的數據。 請問問它是否適用於您。

+0

它將節省,但只爲聯合表。 – 2012-08-09 02:07:59

+0

碰到這一個 – 2012-08-10 01:52:07