/**
  * Execute query and every related query to compile records data in tree form - every relation
  * data will be included as sub key.
  *
  * Attention, Selector will cache compiled data tree and not query itself to keep data integrity
  * and to skip data compilation on second query.
  *
  * @return array
  */
 public function fetchData()
 {
     //Pagination!
     $this->applyPagination();
     //Generating statement
     $statement = $this->sqlStatement();
     if (!empty($this->cacheLifetime)) {
         $cacheKey = $this->cacheKey ?: md5(serialize([$statement, $this->getParameters()]));
         if (empty($this->cacheStore)) {
             $this->cacheStore = $this->orm->container()->get(CacheInterface::class)->store();
         }
         if ($this->cacheStore->has($cacheKey)) {
             $this->logger()->debug("Selector result were fetched from cache.");
             //We are going to store parsed result, not queries
             return $this->cacheStore->get($cacheKey);
         }
     }
     //We are bypassing run() method here to prevent query caching, we will prefer to cache
     //parsed data rather that database response
     $result = $this->database->query($statement, $this->getParameters());
     //In many cases (too many inloads, too complex queries) parsing can take significant amount
     //of time, so we better profile it
     $benchmark = $this->benchmark('parseResult', $statement);
     //Here we are feeding selected data to our primary loaded to parse it and and create
     //data tree for our records
     $this->loader->parseResult($result, $rowsCount);
     $this->benchmark($benchmark);
     //Memory freeing
     $result->close();
     //This must force loader to execute all post loaders (including ODM and etc)
     $this->loader->loadData();
     //Now we can request our primary loader for compiled data
     $data = $this->loader->getResult();
     //Memory free! Attention, it will not reset columns aliases but only make possible to run
     //query again
     $this->loader->clean();
     if (!empty($this->cacheLifetime) && !empty($cacheKey)) {
         //We are caching full records tree, not queries
         $this->cacheStore->set($cacheKey, $data, $this->cacheLifetime);
     }
     return $data;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function container()
 {
     if (empty($this->orm)) {
         return parent::container();
     }
     return $this->orm->container();
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 protected function container()
 {
     return $this->orm->container();
 }