Create INSERT INTO query
public insertInto ( string $table, array $values = [] ) : InsertQuery | ||
$table | string | |
$values | array | - accepts one or multiple rows, @see docs |
Résultat | InsertQuery |
/** * Performs an insert query using FluentPDO */ public function insert($table, array $data) { if (!$this->isConnected()) { static::connect(); } $this->num_queries++; return $this->fpdo->insertInto($table)->values($data)->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 insert($table, $values = []) { return $this->db->insertInto($table, $values); }
}); // GET /persons/:id $app->get('/persons/:id', function ($id) use($app, $fpdo) { $person = $fpdo->from('persons')->where('id', $id)->fetch(); if (empty($person)) { throw new \Exception('Person with id ' . $id . ' does not exist.'); } $app->render(200, $person); }); // POST /persons $app->post('/persons', function () use($app, $fpdo) { $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.'); }
<?php require 'vendor/autoload.php'; require 'Random.php'; // Ukljuci debug // ini_set('display_errors',1); // ini_set('display_startup_errors',1); // error_reporting(-1); use sweelix\guid\Guid; use Slim\Slim; use SlimJson\Middleware; $app = new Slim(); // Dodaj JSON midleware globalno $app->add(new Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true))); // Povezi se sa bazom $pdo = new PDO("sqlite:db.sqlite"); $fpdo = new FluentPDO($pdo); // API za generisanje slucajnog broja $app->get('/match(/:id)', function ($id = '%NO-DATA%') use($app, $fpdo) { if ($id == '%NO-DATA%') { $id = Guid::v4(); } $match = $fpdo->from('matches')->where('id', $id)->fetch(); if (!$match) { $data = ['id' => $id, 'player1_id' => NULL, 'player2_id' => NULL, 'data' => implode(';', Random::generate($id, 500, 0, 100))]; $query = $fpdo->insertInto('matches', $data)->execute(); $match = $data; } $app->render(200, $match); }); $app->run();