j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

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

Closed Surt closed 11 years ago

Surt commented 11 years ago
 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;
        }
Surt commented 11 years ago

A new change for Paris, related to the change in Idiorm. Using this intermediate function we can delete the "find_many" overload in paris. 1 loop less in each Paris call!!! it gives Paris much more speed. It creates the orm instance, and the model after it. In the same loop that Idiorm uses....

        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]);
                $row = $this->_create_model_instance($row);
                $key = (isset($row->{$this->_instance_id_column})) ? $row->{$this->_instance_id_column} : $i;
                $instances[$key] = $row;
            }

            return $instances;
        }
Surt commented 11 years ago

https://github.com/j4mie/idiorm/pull/133

Surt commented 11 years ago

Thanks to the associative keys change, resultsets can use the keys to build, for example, a query to delete all, in just 1 query, or update... instead of call the function on each model.

lrlopez commented 11 years ago

Nice! I'd like to use this feature on my current project. Sorry if this is a little off-topic, but, when is the next point release expected?

treffynnon commented 11 years ago

@lrlopez It was going to be this week, but I am snowed under with a client project at the moment. I am still aiming to have 1.4 out in June though.

treffynnon commented 11 years ago

Merged in commits 022d328fcd8275, 39f4cc4c43b6ee, 42d8715e3bd20, 98a3f0b713ca