I was recently doing some tests regarding the model relationships and I noticed that when there is a Has Many Pivot relationship, the relationship key in the returning object is not an array of objects, but an object itself (an "object of objects" you might say).
For example, let's say I have a 'User' model and a 'User_group' model, and in my database I have a pivot table where I store the user ID and the group ID.
So, if I wanted to retrieve a user with its group(s), I would set the following relationship in the 'User_group' model:
So, if there's a pivot table, the result is pushed into the $subs array at a certain key ($the_foreign_key), whereas if there's a 'has_many' relationship it is only pushed into the array without specifying any key.
Later on, in the _prep_after_read() function, this line causes the relationship data to be transformed into an object because of having specific keys:
json_decode(json_encode($data), FALSE)
I apologize in advance for the length of this post, but I wanted to be as clear as possible and also to address the problem top to bottom.
Hello,
I was recently doing some tests regarding the model relationships and I noticed that when there is a Has Many Pivot relationship, the relationship key in the returning object is not an array of objects, but an object itself (an "object of objects" you might say).
For example, let's say I have a 'User' model and a 'User_group' model, and in my database I have a pivot table where I store the user ID and the group ID.
So, if I wanted to retrieve a user with its group(s), I would set the following relationship in the 'User_group' model:
Now, if I var_dump() the get() function for the user with ID 1, I get the following (FYI, this is just a test table):
As you can see, if I wanted to access the first group of the user like $user -> groups[0], that would give me an error.
I actually think I've found the lines where this 'bug' (I'm just assuming it's a bug) is originating: In the join_temporary_results() function
So, if there's a pivot table, the result is pushed into the $subs array at a certain key ($the_foreign_key), whereas if there's a 'has_many' relationship it is only pushed into the array without specifying any key.
Later on, in the _prep_after_read() function, this line causes the relationship data to be transformed into an object because of having specific keys: json_decode(json_encode($data), FALSE)
I apologize in advance for the length of this post, but I wanted to be as clear as possible and also to address the problem top to bottom.