Skip to content

Instantly share code, notes, and snippets.

@converge
Created April 6, 2015 21:27
Show Gist options
  • Save converge/18686731feeca17836f5 to your computer and use it in GitHub Desktop.
Save converge/18686731feeca17836f5 to your computer and use it in GitHub Desktop.
public function add()
{
$userId = null;
$client = $this->Clients->newEntity();
$client = $this->Clients->patchEntity($client, $this->request->data);
if ($this->request->is('post')) {
$user = $this->Clients->User->newEntity();
$user->role_id = 2;
$user->username = $this->request->data['cnpj'];
$user->password = substr($this->request->data['cnpj'], 0, 4);
if ($this->Client->User->save($user)) {
$userId = $this->Client->User->getLastInsertId();
}
}
if (isset($userId) && $this->request->is('post')) {
$client = $this->Clients->patchEntity ( $client, $this->request->data );
$client->user_id = $userId;
if ($this->Clients->save ($client)) {
$this->Flash->success ('The client has been saved.');
return $this->redirect (['action' => 'index']);
} else {
$this->Flash->error ( 'The client could not be saved. Please, try again.' );
}
}
$regions = $this->getRegions();
$this->set(compact('client', 'regions'));
$this->set('_serialize', ['client']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment