Cakephp HABTM保存数据问题
我有一个功能公司简介有一个HABTM与用户控制器的关系,有一个联合表COMPANYS\U Users我的功能是Cakephp HABTM保存数据问题,cakephp,Cakephp,我有一个功能公司简介有一个HABTM与用户控制器的关系,有一个联合表COMPANYS\U Users我的功能是 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->Aut
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]=>
[类别id]=>1
[名称]=>Infoperks解决方案
[说明]=>网站开发
[用户id]=>1
)
)使用
'deep'=>true
可以保存联接表数据,前提是指定了模型关联关系。您可以尝试使用以下语法保存连接的数据:
$this->User->save($newcompany, array('deep' => true));
//instead of
//$this->User->Company->save($newcompany);
肯定会帮助您保存与HABTM相关的数据。
请询问它是否对您有效。Array([Company]=>Array([id]=>[category_id]=>1[name]=>Infoperks Solutions[description]=>Web development[user_id]=>1)是缺少id还是您键入时出错?是的,它还没有id,因为我在保存之前就已将其保存,但仅用于联合表。