Skip to content

associative fix for #156#162

Closed
Surt wants to merge 3 commits intoj4mie:regression.find_many_id_keysfrom
Surt:regression.find_many_id_keys
Closed

associative fix for #156#162
Surt wants to merge 3 commits intoj4mie:regression.find_many_id_keysfrom
Surt:regression.find_many_id_keys

Conversation

@Surt
Copy link
Copy Markdown
Contributor

@Surt Surt commented Oct 7, 2013

#156

What I did is:

  • new methods to define the result as associative or not (non_associative, associative, reset_associative
  • on the constructor I set the flag to the defined in config self::$_config[$this->_connection_name]['find_many_primary_id_as_key']
  • on find_many check :
    • if there is a primary key on the result instance (we assume primary key is unique)
    • if we look for an associative result set or not. (defined by the new config param and the new methods (non_associative, associative, reset_associative)

So, the config dictates the behaviour to return or not associative keys. That behaviour could be changed during fluent query. If there isn't a primary key define it fallbacks to non associative result set.

Surt added 2 commits October 7, 2013 22:45
… associative methods) AND there is a primary key defined in the result.
@Surt
Copy link
Copy Markdown
Contributor Author

Surt commented Oct 7, 2013

With this commits is easy to fix the has_many_through
see below:

@Surt
Copy link
Copy Markdown
Contributor Author

Surt commented Oct 9, 2013

We must change in https://github.com/j4mie/paris/blob/master/paris.php#L128

   /**
         * Create instances of each row in the result and map
         * them to an associative array with the primary IDs as
         * the array keys.
         * @param array $rows
         * @return array
         */
        protected function _instances_with_id_as_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}) && $this->_associative_results) ? $row->{$this->_instance_id_column} : $i;
                $instances[$key] = $row;
            }
            return $instances;
        }

And the last change, in Model, https://github.com/j4mie/paris/blob/master/paris.php#L388

       ->where("{$join_table_name}.{$key_to_base_table}", $this->$base_table_id_column)
       ->non_associative();

Until this patchs, Paris is broken on has_many_through, returning just 1 instance of multiple objects with the same id (the objects are pushed on the same key)

@treffynnon
Copy link
Copy Markdown
Collaborator

See issue #133

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

Successfully merging this pull request may close these issues.

2 participants