Пример #1
0
 /**
  * Persists the given person to the database
  * 
  * @param \Foodalizr\Model\Person $person
  * @return \Foodalizr\Model\Person
  */
 public function save(Model\Person $person)
 {
     if (null === $person->getId()) {
         $query = 'INSERT INTO `person` (`name`) VALUES (:name);';
         $stmt = $this->getDb()->prepare($query);
     }
     else {
         $query = 'UPDATE `person` SET `name` = :name WHERE `id` = :id;';
         $stmt = $this->getDb()->prepare($query);
         $stmt->bindParam(':id', $person->getId(), \PDO::PARAM_INT);
     }
     $stmt->bindParam(':name', $person->getName(), \PDO::PARAM_STR);
     
     $this->executeStatement($stmt);
     
     if (1 != $stmt->rowCount()) {
         throw new \OutOfBoundsException();
     }
     
     if (null == $person->getId()) {
         $person->setId($this->getDb()->lastInsertId());
     }
     
     return $person;
 }
 private function postAction(Http\Request $request, Http\Response $response)
 {
     $response->addHeader(new Http\Header('Content-Type', 'application/json; charset=utf-8'));
     
     try {
         $person = new Model\Person();
         $person->setName($request->getPost('name'));
         /* @var $personMapper \Foodalizr\Mapper\PersonMapper */
         $personMapper = $this->getMapper('\\Foodalizr\\Mapper\\PersonMapper');
         $personMapper->save($person);
         
         $response->setContent(new \Knid\Json\Model($person));
     }
     catch (\OutOfBoundsException $e) {
         $reponse->setContent(json_encode($e));
     }
     
     return $response;
 }