Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How come when I save a user their user_id changes to the id of whomever is logged in.
  • Hi Everyone!

    Fairly new to Fuel PHP, we are using it as a backend for our iOS application.  I'm running into a weird situation where I am updating the property of a User Model and the user_id is being overwritten by the user_id of the person that is logged in when I save that user.

    We think it may be related to something with ORM or the relationships that we have set up but this is the only model that I am experiencing this with.

    I can provide any code snippets as needed, just ask.

    If anyone has any advice I would greatly appreciate it.

    Thanks!
    Brandon
  • Most obvious candidate is an observer that runs on save, and updates it. So check the model for defined observers.

     It's possible if the developer has used some of our examples, that use this in addition to updated_at to store the last update timestamp.
  • Would it be because of something like this?

    'Orm\\Observer_Self' => array(
    'events' => array('before_insert', 'before_update'),
    'property' => 'user_id',
    ),

    I'm not exactly sure how observers work, I just find it super odd it would change something like the user_id.

    Okay, after commenting these lines out, it now seems to stop the unwanted behavior but will this cause problems elsewhere through the application?
  • I can't comment on it being odd, you should ask the developer about why it was implemented like this. Observers are standard ORM functionality, and very useful if you want event driven actions on ORM objects.

    Since Observer_Self is used, the model will have a before_insert() and before_update() method that will do the actual update. That might cause problems elsewhere if those methods do more than only the update of the user_id property.
  • philipptempelphilipptempel
    Accepted Answer
    Are you using the auth package? If so, there are some observers attached to the models one of them being a let's call it auditor observer. It will automatically update the column 'user_id' of any object so that you can keep track of who last updated that record.

    A while ago (maybe last year of before then) Harro commented on a post about the reason the column 'user_id' exists. It's here http://fuelphp.com/forums/discussion/comment/15748
  • That should not be a problem, in that case, the user_id column is there to store the last-updated user, and nothing else.

    In other words, the "user_id" column is NOT the id of the user defined by that record.
  • Ah we are totally doing this wrong then.  We have been using the user_id as a key for the user.  I didn't realize that this column was who update the record, I guess it kind of makes sense.  We'll just have to come up with some different name for the column or remove the observer.

    Thanks for your help!
  • By default fuel models use the 'id' column as the auto_increment column, which for the users table represents the users' id.

Howdy, Stranger!

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

In this Discussion