Example #1
0
 /**
  * Получение данных из базы
  * @return array
  */
 public function fetchData()
 {
     if (!$this->is_used_fetch) {
         $this->is_used_fetch = true;
         $select = new Utils\SqlParser\Select($this->sql);
         if (!empty($this->search) && !empty($this->sessData['search'])) {
             foreach ($this->sessData['search'] as $key => $search_value) {
                 $search_column = $this->search[$key];
                 if ($search_column instanceof Search) {
                     $search_field = $search_column->getField();
                     switch ($search_column->getType()) {
                         case 'date':
                         case 'datetime':
                             if (!empty($search_value[0]) && empty($search_value[1])) {
                                 $quoted_value = $this->db->quote($search_value[0]);
                                 $select->addWhere("{$search_field} >= {$quoted_value}");
                             } elseif (empty($search_value[0]) && !empty($search_value[1])) {
                                 $quoted_value = $this->db->quote($search_value[1]);
                                 $select->addWhere("{$search_field} <= {$quoted_value}");
                             } elseif (!empty($search_value[0]) && !empty($search_value[1])) {
                                 $quoted_value1 = $this->db->quote($search_value[0]);
                                 $quoted_value2 = $this->db->quote($search_value[1]);
                                 $select->addWhere("{$search_field} BETWEEN {$quoted_value1} AND {$quoted_value2}");
                             }
                             break;
                         case 'select':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} = {$quoted_value}");
                             }
                             break;
                         case 'multiselect':
                             if (!empty($search_value)) {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} IN ({$quoted_value})");
                             }
                             break;
                         case 'text':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote('%' . $search_value . '%');
                                 $select->addWhere("{$search_field} LIKE {$quoted_value}");
                             }
                             break;
                         case 'radio':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} = {$quoted_value}");
                             }
                             break;
                         case 'checkbox':
                             if (!empty($search_value)) {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} IN ({$quoted_value})");
                             }
                             break;
                     }
                 }
             }
         }
         if (!empty($this->sessData['order'])) {
             $select->setOrderBy($this->sessData['order'] + 1 . ' ' . $this->sessData['order_type']);
         }
         if (!empty($this->current_page)) {
             if ($this->current_page == 1) {
                 $select->setLimit($this->records_per_page);
             } elseif ($this->current_page > 1) {
                 $offset = ($this->current_page - 1) * $this->records_per_page;
                 $select->setLimit($this->records_per_page, $offset);
             }
         }
         if (!$this->table) {
             $this->setTable($select->getTable());
         }
         $sql = $select->getSql();
         if ($this->round_record_count) {
             $explain = $this->db->fetchAll('EXPLAIN ' . $sql, $this->sql_params);
             $this->record_count = 0;
             foreach ($explain as $value) {
                 if ($value['rows'] > $this->record_count) {
                     $this->record_count = $value['rows'];
                 }
             }
             $result = $this->db->fetchAll($sql, $this->sql_params);
         } else {
             $result = $this->db->fetchAll("SELECT SQL_CALC_FOUND_ROWS " . substr(trim($sql), 6), $this->sql_params);
             $this->record_count = $this->db->fetchOne("SELECT FOUND_ROWS()");
         }
         if (!empty($result)) {
             foreach ($result as $key => $row) {
                 $this->data[$key] = new Row($row);
             }
         }
     }
     return $this->data;
 }