toArray(); $params = [ ':status' => $data['status'], ':name' => $data['name'], ':position' => $data['position'], ]; try { $statement = $this->connection->prepare( "INSERT INTO {$this->table} (`status`, `name`, `position`) VALUES (:status, :name, :position)" ); $res = $statement->execute($params); if (!$res) { throw new QueryExecutionException('Unable to add data in ' . __CLASS__); } return $this->connection->lastInsertId(); } catch (\Exception $e) { throw new QueryExecutionException('Unable to add data in ' . __CLASS__); } } /** * @param int $id * @param Category $entity * * @return mixed * @throws QueryExecutionException */ public function update($id, $entity) { $data = $entity->toArray(); $params = [ ':status' => $data['status'], ':name' => $data['name'], ':position' => $data['position'], ':id' => $id ]; try { $statement = $this->connection->prepare( "UPDATE {$this->table} SET `status` = :status, `name` = :name, `position` = :position WHERE id = :id" ); $res = $statement->execute($params); if (!$res) { throw new QueryExecutionException('Unable to save data in ' . __CLASS__); } return $res; } catch (\Exception $e) { throw new QueryExecutionException('Unable to save data in ' . __CLASS__); } } /** * @return Collection * @throws InvalidArgumentException * @throws QueryExecutionException */ public function getAllIndexedById() { try { $statement = $this->connection->query($this->selectQuery() . ' ORDER BY position ASC'); $rows = $statement->fetchAll(); } catch (\Exception $e) { throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); } $collection = new Collection(); foreach ($rows as $row) { $collection->addItem( call_user_func([static::FACTORY, 'create'], $row), $row['id'] ); } return $collection; } }