Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
#/app/controller/BooksController.php
class BooksController extends AppController {
public function index()
{
$this->Book->recursive = 1;
$books = $this->Book->find('all');
$this->set('authors', $authors);
}
public function add()
{
if ($this->request->is('post')) {
$this->Book->create();
if ($this->Book->save($this->request->data)) {
$this->Session->setFlash(__('Inserimento avvenuto con successo'));
$this->redirect(array('action'=>'index'));
}
$authors = $this->Book->Author->find('list',array('fields'=>array('id','title')));
$this->set(compact('authors'));
$this->Session->setFlash(__('Inserimento fallito.'));
}
}
public function edit($id = null)
{
$this->Book->id = $id;
if(!$this->Book->exists()) {
$this->setFlash('Libro non trovato','flash-error');
$this->redirect(array('action'=>'index'));
exit;
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Book->save($this->request->data)) {
$this->Session->setFlash(__('Aggiornamento avvenuto con successo'), 'flash-success');
$this->redirect(array('action' => 'index'));
exit;
}
$this->Session->setFlash(__('Problemi in fase di Aggiornamento.'), 'flash-error');
}
$authors = $this->Book->Author->find('list',array('fields'=>array('id','title')));
$this->set(compact('authors'));
$this->set('book', $this->Book->findById($id));
}
public function delete($id = null)
{
$this->layoyt = false;
$this->auotRender = false;
if ($this->request->is('get')) throw new MethodNotAllowedException();
if ($this->Book->delete($id)) {
$this->Session->setFlash(__('Cancellazione avvenuta con successo'), 'flash-success');
$this->redirect(array('action' => 'index'));
exit;
}
$this->Session->setFlash(__('Problemi in fase di eliminazione'), 'flash-error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment