From 04480365e3ad8e75f756e4625622d095c3a607fc Mon Sep 17 00:00:00 2001 From: Erik Wiesenthal Date: Thu, 13 Jun 2013 13:34:02 +0300 Subject: [PATCH 1/3] Added find_many return associative keys. Micro optimizations for Idiorm and Paris Added find_many return associative keys. Micro optimizations for Idiorm and Paris. set and set_expr returns the instance, to admit a fluent query --- idiorm.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/idiorm.php b/idiorm.php index 5b327a61..f8ae762b 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 @@ -1570,6 +1588,7 @@ public function id() { */ public function set($key, $value = null) { $this->_set_orm_property($key, $value); + return $this; } /** @@ -1583,6 +1602,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 +1951,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 From 448e693e58a06507a270d7141613ddf3677917bc Mon Sep 17 00:00:00 2001 From: Erik Wiesenthal Date: Mon, 17 Jun 2013 19:27:22 +0200 Subject: [PATCH 2/3] select method accepts comma separated values It is possible to call the select method this way: ...->select('id,name,title')->...; --- idiorm.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/idiorm.php b/idiorm.php index f8ae762b..6671125d 100644 --- a/idiorm.php +++ b/idiorm.php @@ -711,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 $key=>$column){ + $columns[$key] = $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 From aed9b16288e08881d4f191cd4a67464d6fb2ab37 Mon Sep 17 00:00:00 2001 From: Erik Wiesenthal Date: Mon, 17 Jun 2013 20:38:12 +0300 Subject: [PATCH 3/3] select method accepts comma separated values --- idiorm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/idiorm.php b/idiorm.php index 6671125d..6b97ce8a 100644 --- a/idiorm.php +++ b/idiorm.php @@ -718,8 +718,8 @@ protected function _add_result_column($expr, $alias=null) { */ public function select($column, $alias=null) { $columns = array_map('trim',explode(',',$column)); - foreach($columns as $key=>$column){ - $columns[$key] = $this->_quote_identifier($column); + foreach($columns as $column){ + $column = $this->_quote_identifier($column); $this->_add_result_column($column, $alias); } return $this;