Skip to content

[improvement] find_many returns rows with keys equal to the "id" defined, if exists #132

@Surt

Description

@Surt
 I use the for loop as a microoptimization, a little bit faster than a simple foreach. 

With this change, the find_many will return an array, or resultSet, with keys as the primary key.

array(
'4' => 'aModelinstance'...
'20' => 'anotherModelinstance'...
);

It will save time for the eagerLoading functionality we are preparing for Paris.

   /**
         * Tell the ORM that you are expecting multiple results
         * from your query, and execute it. Will return an array
         * of instances of the ORM class, or an empty array if
         * no rows were returned.
         * @return array
         */
        protected function _find_many() {
            $rows = $this->_run();
            return $this->_instances_with_keys($rows);
        }

        /**
         * Map the rows in keys equal to the id column, instead of no information ones
         */
        protected function _instances_with_keys($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;
        }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions