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 rows with keys equal to the "id" defined, if exists #132

Closed
Surt opened this issue Jun 13, 2013 · 6 comments
Assignees
Milestone

Comments

@Surt
Copy link
Contributor

Surt commented Jun 13, 2013

 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
Copy link
Contributor Author

Surt commented Jun 13, 2013

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
Copy link
Contributor Author

Surt commented Jun 13, 2013

#133

@Surt
Copy link
Contributor Author

Surt commented Jun 14, 2013

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
Copy link
Contributor

lrlopez commented Jun 16, 2013

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
Copy link
Collaborator

@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 treffynnon reopened this Aug 30, 2013
@ghost ghost assigned treffynnon Aug 30, 2013
@treffynnon
Copy link
Collaborator

Merged in commits 022d328, 39f4cc4, 42d8715, 98a3f0b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants