/** * Performs an update query using FluentPDO */ public function update($table, array $data, $key, $keyValue = null) { if (!$this->isConnected()) { static::connect(); } $this->num_queries++; return $this->fpdo->update($table)->set($data)->where($key, $keyValue)->execute(); }
/** * Method used to save entity into it's own database * * @param Entity $entity * * @return bool|int|\PDOStatement * @throws QueryException */ public function save(Entity $entity) { $query = null; if (intval($entity->getId()) > 0 && !$entity->isForceInsert()) { $entity->setDateUpdated(date("Y-m-d H:i:s")); $dto = $entity->getDto(); unset($dto['id']); // No need to "update" ID field $query = $this->queryBuilder->update($this->getTableName(), $dto, $entity->getId()); } else { $entity->setDateCreated(date("Y-m-d H:i:s")); $dto = $entity->getDto(); $query = $this->queryBuilder->insertInto($this->getTableName(), $dto); } try { $result = $query->execute(); } catch (\PDOException $ex) { throw new QueryException($ex, $query); } return $result; }
public function update($table, $set = [], $primaryKey = null) { return $this->db->update($table, $set, $primaryKey); }
$data = $app->request->post(); if (empty($data)) { $data = json_decode($app->request->getBody(), true); } $savedId = $fpdo->insertInto('persons', $data)->execute(); if (!$savedId) { throw new \Exception('Saving person failed.'); } $app->render(200, ['saved' => true, 'id' => $savedId]); }); // PUT /persons/:id $app->put('/persons/:id', function ($id) use($app, $fpdo) { $data = $app->request->post(); if (empty($data)) { $data = json_decode($app->request->getBody(), true); } $updated = $fpdo->update('persons', $data, $id)->execute(); if (!$updated) { throw new \Exception('Person with id ' . $id . ' does not exist.'); } $app->render(200, ['updated' => true, 'id' => $id]); }); // DELETE /persons/:id $app->delete('/persons/:id', function ($id) use($app, $fpdo) { $deleted = $fpdo->deleteFrom('persons', $id)->execute(); if (!$deleted) { throw new \Exception('Person with id ' . $id . ' does not exist.'); } $app->render(200, ['deleted' => $deleted]); }); $app->run();