Skip to content

Instantly share code, notes, and snippets.

@converge
Last active August 29, 2015 14:18
Show Gist options
  • Save converge/4a5637be6772a4c9a06f to your computer and use it in GitHub Desktop.
Save converge/4a5637be6772a4c9a06f to your computer and use it in GitHub Desktop.
#
# ClientsController
#
# I have this Client table, and User table, need to create a user(login/password) when creating a client
#
public function add() {
$client = $this->Clients->newEntity();
if ($this->request->is('post')) {
$client = $this->Clients->patchEntity($client, $this->request->data);
$client = [
'user' => [
'username' => 'teste',
'password' => 'teste'
]
];
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']);
}
@mwdhouse
Copy link

mwdhouse commented Apr 7, 2015

Try

public function add() {
        if ($this->request->is('post')) {
                $this->request->data['user'] = [ 'username' => 'teste', 'password' => 'teste' ];
                $client = $this->Clients->newEntity($this->request->data, [
                    'associated' => [ 'Users' ]
                ]);

            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.');
            }
        } else {
            $client = $this->Clients->newEntity();
        }

        $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