Skip to content

Instantly share code, notes, and snippets.

@cpierce
Created June 30, 2017 04:08
Show Gist options
  • Save cpierce/11e60c9e7bb1e8f3baa20832ca7032c5 to your computer and use it in GitHub Desktop.
Save cpierce/11e60c9e7bb1e8f3baa20832ca7032c5 to your computer and use it in GitHub Desktop.
/**
* Add Method.
*
* @return \Cake\Controller\Controller::redirect
*/
public function add()
{
$table = TableRegistry::get($this->modelClass);
$data = $table->newEntity();
$name = Inflector::singularize($this->modelClass);
if ($this->request->is(['post', 'put'])) {
$data = $table->patchEntity($data, $this->request->data);
if ($table->save($data)) {
$this->Flash->success(__('The ' . $name . ' has been added.'));
return $this->redirect([
'action' => 'index',
]);
}
$this->Flash->error(__('Unable to add the ' . $name . '.'));
}
$template = Configure::read('templates.default');
$this->set(compact('data', 'template'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment