Beispiel #1
0
 public function testIsAssociative()
 {
     $this->assertFalse(\Snowfire\Database\Query\Helper::is_associative(array('a', 'b')));
     $this->assertTrue(\Snowfire\Database\Query\Helper::is_associative(array('a' => 'b')));
 }
Beispiel #2
0
 private function _sqlColumns($array)
 {
     if (is_string($array)) {
         return $array;
     }
     $columns = array();
     if (Helper::is_associative($array)) {
         $array = array($array);
     }
     foreach ($array as $item) {
         if (is_string($item)) {
             $columns[] = Helper::quote_column($item);
         } else {
             $tmp = array();
             foreach ($item as $key => $column) {
                 $tmp[] = $column . " AS `{$key}`";
             }
             $columns[] = implode(', ', $tmp);
         }
     }
     return implode(', ', $columns);
 }
Beispiel #3
0
 /**
  * @param array $p columns, conditions, limit, order_by, id_indexed, value_column, ignore_active_condition, single_column, foreign_models
  */
 public function many($p = array())
 {
     $called_class = get_called_class();
     if (isset($called_class::$_many_default_parameters)) {
         $p = array_merge($called_class::$_many_default_parameters, $p);
     }
     if (isset($p['columns'])) {
         $q = $this->_query_select($p['columns']);
     } else {
         $q = $this->_query_select($this->_get_special_datetime_columns_sql($this->_table . '.*', $this->_special_fields));
     }
     if (isset($this->_active_condition) && empty($p['ignore_active_condition'])) {
         $q->where($this->_active_condition);
     }
     if (isset($p['conditions']) && $p['conditions']) {
         if (is_string($p['conditions'])) {
             $q->where($p['conditions']);
         } else {
             if (is_array($p['conditions']) && Helper::is_associative($p['conditions'])) {
                 $q->where($p['conditions']);
             } else {
                 call_user_func_array(array($q, 'where'), $p['conditions']);
             }
         }
     }
     if (isset($p['limit'])) {
         $q->limit($p['limit']);
     }
     if (isset($p['order_by'])) {
         $q->order_by($p['order_by']);
     }
     if (isset($p['foreign_models']) && $this->_foreign['many_to_many']) {
         $q->group_by($this->_primary_key);
         foreach ($this->_foreign['many_to_many'] as $foreign) {
             if (isset($p['foreign_models'][$foreign['name']])) {
                 $this->_get_many_to_many_pre($q, $p['foreign_models'][$foreign['name']]);
             }
         }
     }
     $single_column = isset($p['single_column']) && $p['single_column'];
     $rows = $q->execute($single_column ? array('single_column' => $single_column) : null);
     $single_row = isset($p['limit']) && $p['limit'] == 1;
     if ($rows && isset($p['foreign_models']) && !$single_column) {
         foreach ($this->_foreign['one_to_many'] as $foreign) {
             if (isset($p['foreign_models'][$foreign['name']])) {
                 $rows = $this->_get_one_to_many($single_row, $rows, $p['foreign_models'][$foreign['name']]);
             }
         }
         foreach ($this->_foreign['one_to_one'] as $foreign) {
             if (isset($p['foreign_models'][$foreign['name']])) {
                 $rows = $this->_get_one_to_one($single_row, $rows, $p['foreign_models'][$foreign['name']], $foreign);
             }
         }
         foreach ($this->_foreign['many_to_many'] as $foreign) {
             if (isset($p['foreign_models'][$foreign['name']])) {
                 $rows = $this->_get_many_to_many_post($single_row, $rows, $p['foreign_models'][$foreign['name']]);
             }
         }
         foreach ($this->_foreign['many_to_one'] as $foreign) {
             if (isset($p['foreign_models'][$foreign['name']])) {
                 $rows = $this->_get_many_to_one($single_row, $rows, $p['foreign_models'][$foreign['name']], $foreign);
             }
         }
     }
     if (!$single_row && !$single_column && (isset($p['id_indexed']) || isset($p['value_column']))) {
         $result = array();
         foreach ($rows as $row) {
             $v = isset($p['value_column']) ? $row[$p['value_column']] : $row;
             if (isset($p['id_indexed'])) {
                 $result[$row[$this->_primary_key]] = $v;
             } else {
                 $result[] = $v;
             }
         }
         return $result;
     } else {
         return $rows;
     }
 }