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....