Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Many to many relation not saved for current user
  • 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...

                        $permissions = \Model_Permission::find('all');
                        foreach($permissions as $permission):
                            if(in_array($permission->id, Input::post('permissions', array())))
                            {
                                $user->permissions[$permission->id] = $permission;
                            }
                            else
                            {
                                unset($user->permissions[$permission->id]);
                            }
                        endforeach;
                        $user->save();
  • If you enable the profiler, which queries are fired when you save?
    And which FuelPHP version are you on?

    I don't see a reason (in this bit of code) why it would work for user A but not for user B.
  • I am on 1.5, I will launch profiler...

    >>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...

                        $permissions = \Model_Permission::find('all');
                        foreach($permissions as $permission):
                            if(in_array($permission->id, Input::post('permissions', array())))
                            {
                                $user->permissions[$permission->id] = $permission;
                            }
                            else
                            {
                                unset($user->permissions[$permission->id]);
                            }
                        endforeach;

                        $roles = \Model_Role::find('all');                   
                        foreach($roles as $role):
                            if(in_array($role->id, Input::post('roles')))
                            {
                                $user->roles[$role->id] = $role;
                            }
                            else
                            {
                                unset($user->roles[$role->id]);
                            }
                        endforeach;

                        $user->save();

    Roles make no problem, while permissions above do as I described.
  • You're sure the find() returns something?

    It would be weird if this would be broken, everyone using ORM would be all over us...
  • Yes I am sure... It finds all existing permissions from permissions table.

    Bellow is the profiler, SQLs when my update action is called https://dl.dropbox.com/u/1207859/Screen Shot 2013-03-28 at 5.32.26 PM.png

    Entire action code of this action can be seen here: https://gist.github.com/PrimozRome/24e98a1822941f4fddd3

    The permission relation update query is nowhere to be seen ...
  • If I execute the same action on some other user then currently logged in user, I get the relation queries...

    https://dl.dropbox.com/u/1207859/Screen Shot 2013-03-28 at 5.43.04 PM.png
  • Absolutely bizar...

    And if you login with another user, the problem moves to that user?
  • >And if you login with another user, the problem moves to that user?
    Hmm just tried and its ok with other user :S... I don't get it!
  • So it's only with only one user record? And not related to "the current logged in user"?

    Weird. No database activity at all? What if you do an implicit:

    unset($user->permissions);

    before you start your loop?
  • Looks like only one user record... Totally weird indeed.
  • >>Weird. No database activity at all? What if you do an implicit:
    >>unset($user->permissions);

    No DB activity, tried the explicit unset, same result...
  • 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.
  • And that new first row has a different primary key from the old first row?
  • yes... first one had primary key 1, the new one has 178
  • Absolutely clueless. I can't think of any reason why the record positiion in a table would affect inserting data in another table.

    The only thing I can think of is debugging the save method of the ORM's manymany class, and try to figure out why they are not being saved.

    Something must be different somewhere...
  • 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.
  • I can't imagine.

    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.


  • what file is that?
  • fuel/packages/orm/classes/model.php
  • 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 :-/.
  • If you're willing to zip your entire fuel install root and a dump of your database, and get it to me, I can have a look.

    You can reach me at wanwizard<at>fuelphp.com or on IRC if you don't want it public.
  • Wow that would be very nice of you. I can do that for sure. Where can I send you the download  link?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion