Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HABTM saving data problems

Tags:

cakephp

I have a function Company Profile has a relationship HABTM to Users Controller with a joint table companies_users my functions is

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'));

}

why is it it doesn't save on the joint table ? please help me on how it will be save in the joint table currently i have this array

Array ( [Company] => Array ( [id] => [category_id] => 1 [name] => Infoperks Solutions [description] => Web development [user_id] => 1 )

)

like image 294
Daemon Developer Avatar asked May 20 '26 13:05

Daemon Developer


1 Answers

Using 'deep' => true you can save the joined table data provided that you specified the model associationship. You can try to save the joined data using the following syntax:

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

This link will surely help you to save your HABTM associated data. Kindly ask if it not worked for you.

like image 99
Arun Jain Avatar answered May 22 '26 12:05

Arun Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!