/** * Executes this query and returns the result * @param boolean $indexField Name of the field to use as key in the resulting array, default is id * @return array Array with data objects */ public function query($indexField = null) { $queryId = $this->getQueryId($indexField); $result = null; if ($this->willCacheResult) { $resultId = $this->getResultId($queryId); $cachedResult = $this->cache->getResult($resultId); if ($cachedResult) { $result = $cachedResult->getResult(); $belongsTo = $cachedResult->getBelongsToFields(); $has = $cachedResult->getHasFields(); } } if (!$result) { $connection = $this->model->getMeta()->getConnection(); $cachedQuery = $this->cache->getQuery($queryId); if (!$cachedQuery) { $statement = self::$queryParser->parseQuery($this); $statementParser = $connection->getStatementParser(); $sql = $statementParser->parseStatement($statement); $belongsTo = self::$queryParser->getRecursiveBelongsToFields(); $has = self::$queryParser->getRecursiveHasFields(); $usedModels = $this->getUsedModels($statement); $cachedQuery = new QueryCacheObject($sql, $usedModels, $belongsTo, $has); $this->cache->setQuery($queryId, $cachedQuery); } else { $sql = $cachedQuery->getSql(); $belongsTo = $cachedQuery->getBelongsToFields(); $has = $cachedQuery->getHasFields(); $usedModels = $cachedQuery->getUsedModels(); } $sql = $this->parseVariablesIntoSql($sql, $connection); $result = $connection->execute($sql); if ($this->willCacheResult) { $cachedResult = new ResultCacheObject($result, $belongsTo, $has); $this->cache->setResult($resultId, $cachedResult, $usedModels); } } $result = $this->parseResult($result, $belongsTo, $has, $indexField); return $result; }