HABTM保存数据问题

时间:2012-08-07 07:20:40

标签: cakephp

我有一个功能公司简介有一个关系HABTM到用户控制器与联合表companies_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->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             [name] => Infoperks解决方案             [描述] => Web开发             [user_id] => 1         )

1 个答案:

答案 0 :(得分:1)

使用'deep' => true,您可以保存已指定模型关联的连接表数据。您可以尝试使用以下语法保存联接数据:

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

This link肯定会帮助您保存HABTM相关数据。 请问它是否对你不起作用。