Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improvement] find_many returns an associative array #133

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummm... maybe it is no so simple. I think $alias parameter should be a list of aliases, shouln't it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks like you have a point there. if the select is an array of columns then it should be in the form:

$columns = array(
    'alias' => 'column_name',
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, on that particular commit I added the select to accepts comma separated values, but in that case you can't provide aliases, or it will fail. So you can use it as always, even using $alias or you can call it like:
->select('id, name,title, content') not using aliases.
There is no check if you are using the function on the right way so if you send a comma separated value and an alias you will have an unexpected behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unique use of that commit was to reduce the notation to take multiple fields. However select_many does it already. (using more , and ` but in the right way.

}
return $this;
}


/**
* Add an unquoted expression to the list of columns returned
* by the SELECT query. The second optional argument is
Expand Down Expand Up @@ -1570,6 +1595,7 @@ public function id() {
*/
public function set($key, $value = null) {
$this->_set_orm_property($key, $value);
return $this;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down