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

associative fix for #156 #162

Conversation

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

Surt commented Oct 7, 2013

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

@Surt
Copy link
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
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.

None yet

2 participants