/** * Performs select query and, if required, hydrates the response to target class. * * @param string $query Query to perform * @param array $params Query parameters * @param bool $single_result If TRUE, only one result (first) is returned * @param null|string $hydrate_class If set, the class will be hydrated from result (using Hydrater) * @param null|string $hydrate_map_id Identifier of hydration map (or null if none) * @param null|string $error_message Message to display, if query fails * * @return mixed Hydrated class, array if no hydration or FALSE if not found. * @throws DataException */ protected function select($query, $params = [], $single_result = false, $hydrate_class = null, $hydrate_map_id = null, $error_message = null) { Console::debug("Performing query: %s", $query); $this->logQueryParameters($params, Console::DEBUG); $stmt = $this->pdo->prepare($query); if (!$stmt || !$stmt->execute($params)) { $this->logDatabaseError($params); throw new DataException($error_message ?: "Unable to perform select query."); } $result = []; while ($values = $stmt->fetch(PDO::FETCH_ASSOC)) { $single = $values; if (is_string($hydrate_class)) { $single = new $hydrate_class(); Hydrator::sharedHydrator()->fromArray($single, $values, $hydrate_map_id); } $result[] = $single; if ($single_result) { // if only one result is requested, // break out after first result read break; } } // if there is some result, return it; // otherwise, return false if (count($result)) { return $single_result ? $result[0] : $result; } return false; }
public function toArray($map_id = null, $hints_only = false) { return Hydrator::sharedHydrator()->toArray($this, $map_id, $hints_only); }