public function compile($db = NULL) { if ($this->context_model() and $meta = Jam::meta(Jam_Query_Builder::aliased_model($this->context_model()))) { $db = Database::instance($meta->db()); } $original_on = $this->_on; $original_using = $this->_using; $original_table = $this->_table; if (!empty($this->_on)) { foreach ($this->_on as &$condition) { $condition[0] = Jam_Query_Builder::resolve_attribute_name($condition[0], $this->model() ? $this->model() : $this->_table); $condition[2] = Jam_Query_Builder::resolve_attribute_name($condition[2], $this->context_model()); } } $this->_table = Jam_Query_Builder::resolve_table_alias($this->_table); if (!empty($this->_using)) { foreach ($this->_using as &$column) { $column = Jam_Query_Builder::resolve_attribute_name($column, $this->meta()); } } $additional_joins = ''; foreach ($this->_joins as $join) { $additional_joins .= ' ' . $join->compile($db); } $compiled = parent::compile($db) . $additional_joins; $this->_on = $original_on; $this->_using = $original_using; $this->_table = $original_table; return $compiled; }
protected function _compile_conditions(Database $db, array $conditions) { foreach ($conditions as &$group) { foreach ($group as &$condition) { $condition[0] = Jam_Query_Builder::resolve_attribute_name($condition[0], $this->meta()->model(), $condition[2]); } } return parent::_compile_conditions($db, $conditions); }
public function aggregate_query($function, $column = NULL) { if ($column === NULL or $column === '*') { $column = '*'; } else { $db = Database::instance($this->meta()->db()); $column = Jam_Query_Builder::resolve_attribute_name($column, $this->meta()->model()); $column = $db->quote_column($column); } $count = clone $this; return $count->select(array(DB::expr("{$function}({$column})"), 'result')); }
/** * @dataProvider data_resolve_attribute_name */ public function test_resolve_attribute_name($name, $context_model, $expected) { $this->assertEquals($expected, Jam_Query_Builder::resolve_attribute_name($name, $context_model)); }
/** * Return all of the models in the result as an array. * * // Indexed array of all models * $rows = $result->as_array(); * * // Associative array of models by "id" * $rows = $result->as_array('id'); * * // Associative array of fields, "id" => "name" * $rows = $result->as_array('id', 'name'); * * @param string column for associative keys * @param string column for values * @return array */ public function as_array($key = NULL, $value = NULL) { $key = Jam_Query_Builder::resolve_meta_attribute($key, $this->meta()); if ($value === NULL) { return array_map(array($this, '_load_model'), $this->result()->as_array($key)); } else { $value = Jam_Query_Builder::resolve_meta_attribute($value, $this->meta()); return $this->result()->as_array($key, $value); } }
protected static function find_or($method, $model, array $values) { $collection = Jam::all($model); $converted_keys = array(); foreach ($values as $key => $value) { $key = Jam_Query_Builder::resolve_meta_attribute($key, Jam::meta($model), $value); $collection->where($key, '=', $value); $converted_keys[$key] = $value; } if ($item = $collection->first()) { return $item; } return call_user_func($method, $model, $converted_keys); }
public function as_array($key = NULL, $value = NULL) { $results = array(); $key = Jam_Query_Builder::resolve_meta_attribute($key, $this->meta()); $value = Jam_Query_Builder::resolve_meta_attribute($value, $this->meta()); foreach ($this as $i => $item) { $results[$key ? $item->{$key} : $i] = $value ? $item->{$value} : $item; } return $results; }
/** * Generate Jam_Query_Builder_Join based on the given arguments * @param string $table * @param string $type LEFT, NATURAL... * @param string $context_model the model of the parent * @param boolean $resolve_table_model wether to resolve the name of the model to a tablename * @return Jam_Query_Builder_Join */ public static function resolve_join($table, $type = NULL, $context_model = NULL, $resolve_table_model = TRUE) { $context_model_name = Jam_Query_Builder::aliased_model($context_model); if ($resolve_table_model and is_string($context_model_name) and $meta = Jam::meta($context_model_name)) { $table_name = Jam_Query_Builder::aliased_model($table); if (is_string($table_name) and $association = $meta->association($table_name)) { return $association->join(is_array($table) ? $table[1] : NULL, $type); } } $join = Jam_Query_Builder_Join::factory($table, $type); if ($context_model) { $join->context_model($context_model); } return $join; }