user4458505
user4458505

Reputation: 217

CakePHP 3.0 Saving My Associated Data

What I am trying to do is build a new user reg. input, now I have some of this working but can not seem to save my company in the way the new docs say.

Now I am not sure if I am just not understanding the docs on the CakePHP site but I can't seem to save associated data into a new table, then get its ID to put into my users table.

So my code

public function add() {
    $this->loadModel('Companies');
    $user = $this->Users->newEntity();
   // $company = $this->Companies->newEntity();

    if ($this->request->is('post')) {
        //$company = $this->Companies->patchEntity($company, $this->request->data['company']);
        $user = $this->Users->patchEntity($user, $this->request->data, [ 'associated' => 'Company.name' ]);

        //debug($company);
        debug($this->Users->save($user));
        die();

        if ($this->Companies->save($company)) {
            debug($company);
            debug($user);
            die();

            if ($this->Users->save($user)) {
                $this->Flash->success('The user has been saved.');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error('The user could not be saved. Please, try again.');
            }

        } //End of 'save' for company name


    }

    $userlevels = $this->Users->Userlevels->find('list', ['keyField' => 'id', 'valueField' => 'usertitle']);

    $this->set(compact('user', 'userlevels'));
    $this->set('_serialize', ['user']);
}

MY database, which is MySQL, has a users table with a 'company_id' field in it. So I have done a test and taken the company name the user inputs on my form and save that, if I debug that it saves fine and gets me back my new id of the saved data in the properties section.

But from what I read, I should be able to save that within my user save? I have still added the companies model, not sure if i need that?

But when I debug the save to the user, it saves the user but not the a new company?

I can post the other details, if needed but this code as just been edited a little from what cake bakes. I did change the input form on my company to $this->Form->input('company.name');

So end goal, is to be able to save the user details into a new user, and save a company name into a new company record.

So what am I doing wrong?

Please be kind when reading though my question, I am dyslexic, so I might not have explained things right, please tell me if I need to re-word, expand on what I have said, many thanks for understanding....

Upvotes: 0

Views: 277

Answers (1)

user4458505
user4458505

Reputation: 217

Ok, seems I have solved my own problem, but not sure if its the best solution. So if anyone else has a different method, please let me know.

So I re-read the Cake docs, (again...), and pulled in TableRegistry and this seems to work.

So my code is currently,

 $UserData = TableRegistry::get('Users');
 $user = $UserData->newEntity( $this->request->data );

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

    debug( $UserData->save($user) ); 

 }

This saves the new company name, in my companies table and places the ID into my users table. More testing is needed, but I thought I would post this to a) get some feedback to see if this is indeed is the right thing to do b) to help anyone else looking at doing this.

Thanks,

Upvotes: 1

Related Questions