/** * @param PostInterface $postObject * * @return PostInterface * @throws \Exception */ public function save(PostInterface $postObject) { $postData = $this->hydrator->extract($postObject); unset($postData['id']); //Neither Insert nor Update needs the ID in the array if ($postObject->getId()) { //ID present, it's an update $action = new Update('post'); $action->set($postData); $action->where(array('id = ?' => $postObject->getId())); } else { // ID not present, it's an Insert $action = new Insert('post'); $action->values($postData); } $sql = new Sql($this->dbAdapter); $stmt = $sql->prepateStatementForSqlObject($action); $result = $stmt->execute(); if ($result instanceof ResultInterface) { if ($newId = $result->getGeneratedValue()) { //when a value has been generated, set it on the object $postObject->setId($newId); } return $postObject; } throw new \Exception("Database error"); }