I have many to many relation set between User_Model and Permission_Model... I have edit form for updating user permissions. It won't save me permissions for currently logged in admin, but it will properly save for all other users... I don't understand why not?
My save code is below. The code logic is ok. It even comes to the line where it supposed to set relation ( $user->permissions[$permission->id] = $permission ) but when $user->save() is called it's not actually written into DB table...
>>I don't see a reason (in this bit of code) why it would work for user A but not for user B. Yeah thats the real question here which has been busting my head for couple of N hours... Because I use exactly the same code for roles, and that works without anyproblem...
Ok now I went into extremes and things got even more complicated. I deleted that user from the DB, with hope once I insert in again things will get going....
This user was the first row in users table. Now check this out. The second user I tried above, that worked, has now become the first row in users table (because I deleted the problematic user). And now I am seeing the same problem with this user? WTF??!?!? So it's not limited to the user above, but to the first user row in users table... now this is totally weird.
Do you think that auth package could have some kind of reason on this? But then again why only with first user in users DB... argghhhhhh. I guess I am gonna try debugging save method like you suggested.
The $user you use, where does that come from? It is fetched from the (users) model in all cases, or does it come from somewhere else? Do you do something special for the current logged in user?
Somewhere something must be different in your code...
I am just debugging save function of manymany.php. Condition where it checks if it needs to INSERT relation is different for problematic user, so it never actually INSERTS relation:
// Check if the model was already assigned, if not INSERT relationships: if ( ! in_array($current_model_id, $original_model_ids)) { ....
So I went and checked the contents of $original_model_ids. For some reason the relation models IDs that it should insert are already in this array for problematic user... So this condition is never true and INSERT is not happening... Any clues?
The $user object passes $this->_original_relations[$rel_name] to it (that's where original_model_ids comes from). $rel_name is the name of the relations, in this case 'permissions'.
This contains the list of related objects, fetched when the user was fetched, or later, when you access $user->permissions for the first time.
So perhaps you have to backtrack, and see what causes these to be present. You're sure the permissions you are trying to assign to this user are not already assigned?
>>So perhaps you have to backtrack, and see what causes these to be present. >>You're sure the permissions you are trying to assign to this user are not already assigned?
Yes I am 100% sure. I am looking at users_relations table right here before I save and the relations are not there. Checked this 10 times. And this is the single controller action in my admin area that sets user-permissions.
I read these (see image) from $user->permissions and this is the state before ->save()... if I check-mark the missing ones, on save they do not get inserted because they are already contained in $original_model_ids ...
The data in $_original_model_ids comes from $this->_original_relations in the model. It is given a value in the method _update_original_relations().
That method is called from _update_original() and from get(), to update after a lazy load. A lazy load is triggered by accessing $user->permissions. _update_original() is called in the constructor (when you create $user), and after $user->save() is called.
So you might have to dive into this, and see what is different between a user that works and one that doesn't.
This is impossible for me to debug ... To many calls to this method and I get lost with what is what and even not sure what I am looking at. I don't know what to do. I guess I am gonna live with it :-/.