diff --git a/idiorm.php b/idiorm.php index 5b327a61..6b97ce8a 100644 --- a/idiorm.php +++ b/idiorm.php @@ -540,9 +540,27 @@ public function find_many() { */ protected function _find_many() { $rows = $this->_run(); - return array_map(array($this, '_create_instance_from_row'), $rows); + return $this->_instances_with_key($rows); } + /** + * Create instances and assigns it to an associative array + * @return array + */ + + protected function _instances_with_key($rows){ + $size = count($rows); + $instances = array(); + for ($i = 0; $i < $size; $i++) { + $row = $this->_create_instance_from_row($rows[$i]); + $key = (isset($row->{$this->_instance_id_column})) ? $row->{$this->_instance_id_column} : $i; + $instances[$key] = $row; + } + + return $instances; + } + + /** * Tell the ORM that you are expecting multiple results * from your query, and execute it. Will return a result set object @@ -693,14 +711,21 @@ protected function _add_result_column($expr, $alias=null) { /** * Add a column to the list of columns returned by the SELECT - * query. This defaults to '*'. The second optional argument is + * query. This defaults to '*'. + * $column can be a string of columns to select separated by comma + * The second optional argument is * the alias to return the column as. */ public function select($column, $alias=null) { - $column = $this->_quote_identifier($column); - return $this->_add_result_column($column, $alias); + $columns = array_map('trim',explode(',',$column)); + foreach($columns as $column){ + $column = $this->_quote_identifier($column); + $this->_add_result_column($column, $alias); + } + return $this; } + /** * Add an unquoted expression to the list of columns returned * by the SELECT query. The second optional argument is @@ -1570,6 +1595,7 @@ public function id() { */ public function set($key, $value = null) { $this->_set_orm_property($key, $value); + return $this; } /** @@ -1583,6 +1609,7 @@ public function set($key, $value = null) { */ public function set_expr($key, $value = null) { $this->_set_orm_property($key, $value, true); + return $this; } /** @@ -1931,6 +1958,22 @@ public function as_array() { public function count() { return count($this->_results); } + + /** + * Get the first element of the result set + * @return Model + */ + public function first(){ + return reset($this->get_results()); + } + + /** + * Get the last element of the result set + * @return Model + */ + public function last(){ + return end($this->get_results()); + } /** * Get an iterator for this object. In this case it supports foreaching