Example #1
0
/**
 * @param string $q
 * @param bool $protect
 * @return Iterator
 */
function q_assoc_iterator($q, $protect = true)
{
    return SQL::q_assoc_iterator($q, $protect);
}
Example #2
0
 /**
  * @param bool $skip_objects_creation - true if no need to create objects
  * @param bool $skip_changed_fields - skip update of changed fields
  * @return $this
  */
 protected function collectObjects($skip_objects_creation = false, $skip_changed_fields = false)
 {
     $sql = $this->getSelectSql();
     if ($this->last_used_sql && $sql === $this->last_used_sql) {
         // Skip queries - nothing changed
         return $this;
     }
     $this->last_used_sql = $sql;
     // Check cache for this exact collection
     if ($this->use_cache) {
         //Check cached values, set local properties
         $data = Cacher::getInstance()->getDefaultCacher()->get($this->getCacheKey($sql));
         if ($data && is_array($data) && isset($data['collected_objects_data'], $data['collected_objects'])) {
             // Set local data
             $this->collected_objects_data = $data['collected_objects_data'];
             $this->collected_objects = $data['collected_objects'];
             // No further actions
             return $this;
         }
     }
     // Use Iterator in DB query
     if ($this->use_iterator) {
         $this->collected_objects_data = SQL::q_assoc_iterator($sql, false);
     } else {
         $this->collected_objects_data = SQL::q_assoc($sql, false);
     }
     if ($this->require_to_count_total_rows) {
         $this->total_count_rows = q_value('SELECT FOUND_ROWS();');
     }
     $this->collected_objects = [];
     // Reset objects
     if (!$skip_objects_creation) {
         // Need to create objects from array data
         foreach ($this->collected_objects_data as $v) {
             $class = $this->getObjectClass();
             /** @var Entity $obj */
             $obj = new $class();
             // Prevent auto-query db, skip tables with no id field
             $id = $v['id'];
             unset($v['id']);
             // Set object data
             $obj->loadDataFromArray($v, $skip_changed_fields);
             // Set current ID
             $obj->setId($id, false);
             // Save in returning array ob objects
             $this->collected_objects[] = $obj;
         }
     }
     if ($this->use_cache) {
         // Save all collected data to Cache
         $data = ['collected_objects_data' => $this->collected_objects_data, 'collected_objects' => $this->collected_objects];
         Cacher::getInstance()->getDefaultCacher()->set($this->getCacheKey($sql), $data, $this->cache_ttl);
     }
     return $this;
 }