connection = $connection; $this->factory = $factory; } public function find($id) { $row = $this->fetchRow($id); if (is_array($row)) { return $this->factory->create($row); } return null; } public function findAll() { return $this->factory->createMany($this->fetchRows()); } /** * @param int $id * @return false|array * @throws \Doctrine\DBAL\DBALException */ private function fetchRow($id) { $result = $this->connection->executeQuery($this->getSQLForSingleRow(), ['id' => $id]); $row = $result->fetch(\PDO::FETCH_ASSOC); $result->closeCursor(); return $row; } private function fetchRows() { $statement = $this->connection->executeQuery($this->getSQLForAllRows()); $rows = $statement->fetchAll(\PDO::FETCH_ASSOC); $statement->closeCursor(); return $rows; } /** * @return string */ private function getSQLForSingleRow() { static $sql; if (!$sql) { $sql = $this->connection->createQueryBuilder() ->select($this->getQuotedFields()) ->from('metadatas_structure', 's') ->where('id = :id') ->getSQL(); } return $sql; } /** * @return string */ private function getSQLForAllRows() { static $sql; if (!$sql) { $sql = $this->connection->createQueryBuilder() ->select($this->getQuotedFields()) ->from('metadatas_structure', 's') ->orderBy('sorter') ->getSQL(); } return $sql; } /** * @return array */ private function getQuotedFields() { $fields = []; foreach (self::$columnNames as $field) { $fields[] = $this->connection->quoteIdentifier($field); }; return $fields; } }