diff --git a/classes/automodeler/core.php b/classes/automodeler/core.php index e3d4072..744cb4c 100644 --- a/classes/automodeler/core.php +++ b/classes/automodeler/core.php @@ -54,7 +54,7 @@ public function __construct($id = NULL) if ($id !== NULL) { - $this->load(db::select_array(array_keys($this->_data))->where('id', '=', $id)); + $this->load(db::select_array(array_keys($this->_data))->where($this->_table_name.'.id', '=', $id)); } } diff --git a/classes/automodeler/orm/core.php b/classes/automodeler/orm/core.php index 03e98ee..95b6845 100644 --- a/classes/automodeler/orm/core.php +++ b/classes/automodeler/orm/core.php @@ -141,15 +141,27 @@ public function load(Database_Query_Builder_Select $query = NULL, $limit = 1) if ($this->_load_with !== NULL) { + if (is_array($this->_load_with)) + { + $model = current(array_keys($this->_load_with)); + $alias = current(array_values($this->_load_with)); + } + else + { + $model = $this->_load_with; + $alias = $this->_load_with; + } + $fields = array(); - foreach (array_merge($this->fields(), AutoModeler_ORM::factory($this->_load_with)->fields()) as $field) + foreach (array_merge($this->fields(), AutoModeler_ORM::factory($model)->fields()) as $field) { $fields[] = array($field, str_replace('.', ':', $field)); } - $query->select_array($fields); - $join_table = inflector::plural($this->_load_with); - $query->join($join_table)->on($join_table.'.id', '=', $this->_table_name.'.'.$this->_load_with.'_id'); + $query->select_array($fields, TRUE); + $join_model = Model::factory($model); + $join_table = $join_model->get_table_name(); + $query->join($join_table)->on($join_table.'.id', '=', $this->_table_name.'.'.$alias.'_id'); } return parent::load($query, $limit); diff --git a/classes/database/query/builder/select.php b/classes/database/query/builder/select.php new file mode 100644 index 0000000..cb5f4d9 --- /dev/null +++ b/classes/database/query/builder/select.php @@ -0,0 +1,51 @@ +_select = array(); + } + + $columns = func_get_args(); + + $this->_select = array_merge($this->_select, $columns); + + return $this; + } + + /** + * Choose the columns to select from, using an array. + * + * @param array list of column names or aliases + * @return $this + */ + public function select_array(array $columns = NULL, $reset = FALSE) + { + if ($reset) + { + $this->_select = array(); + } + + $this->_select = array_merge($this->_select, $columns); + + return $this; + } +} \ No newline at end of file