public function saveTodo(Todo $todo) { $data = array('title' => $todo->getTitle(), 'description' => $todo->getDescription(), 'completed' => $todo->getCompleted()); $id = (int) $todo->getId(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getTodo($id)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception("Todo {$id} doesn't exist"); } } }
private function getTodo($id) { $stmt = $this->app['db']->prepare('SELECT * FROM todo WHERE id = ?'); $stmt->bindValue(1, $id); $stmt->execute(); return Todo::create($stmt->fetch()); }
/** * Créer une nouvelle tâche * */ public function createAction() { $form = new TodoForm(); $request = $this->getRequest(); if ($request->isPost()) { $todo = new Todo(); $form->setInputFilter($todo->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $todo->exchangeArray($form->getData()); $this->getTodoTable()->saveTodo($todo); return $this->redirect()->toRoute('todo'); } } return array('form' => $form); }
public function indexAction() { $form = $this->getTodoForm(); $request = $this->getRequest(); if ($request->isPost()) { if (isset($_POST['action'])) { $todo = new Todo(); switch ($_POST['action']) { case "add_todo": $form->setInputFilter($todo->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $todo->exchangeArray($data); if ($this->getTodoTable()->saveTodo($todo)) { } } break; case "update_todo": $form->setInputFilter($todo->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $todo->exchangeArray($form->getData()); if ($this->getTodoTable()->saveTodo($todo)) { } } break; } } return $this->redirect()->toUrl($_SERVER['REQUEST_URI']); } $todoFilterForm = $this->getTodoFilterForm(); $filter = array(); if (isset($_GET['filter'])) { $todoFilterForm->setData($_GET['filter']); if ($todoFilterForm->isValid()) { $filter = $todoFilterForm->getData(); } } return new ViewModel(array('todos' => $this->getTodoTable()->fetchAll($filter), 'form' => $form, 'todoFilterForm' => $todoFilterForm)); }