Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Querys
  • I have a question regarding Fuel's ORM querys. Are they Lazy querys?
    For example if we have:
    $user = User::find(3);
    $user->first_name = 'Dan';
    $user->save();
    

    When we call $user->save(); will the ORM make only one smart query
    UPDATE users SET first_name='Dan' WHERE id=3;
    
    or it will make two querys, by first getting the row and then updating it?
    SELECT * FROM users WHERE id=3; 
    UPDATE users SET first_name='Dan' WHERE id=3;
    
  • My colleague and I did a port of kohana's ORM for fuel which does proper lazy loading. We added the magic methods, etc... More info: https://github.com/TJS-Technology/fuel-kohana-orm Hope it might help!
  • After testing it, yes it does two querys.
    I would be a nice feature if the ORM would be updated to support lazy querys so we should make only the UPDATE query when we call ->save() if we don't use (read) any of the fiels before calling save() So it should behave like this:
    //This time we don't get first_name's or any other field's value, we only set it's value
    //So we only should have
    
    $user = User::find(3);
    $user->first_name = 'Dan';
    //Only one query: UPDATE users SET first_name='Dan' WHERE id=3;
    $user->save();
    

    vs
    //This time we do get first_name's value and after that we set a new value for it
    //So now we should have
    
    $user = User::find(3);
    //First query: SELECT * FROM users WHERE id=3;
    $somevar = $user->first_name;
    
    $user->first_name = 'Dan';
    //Second query: UPDATE users SET first_name='Dan' WHERE id=3;
    $user->save();
    
  • Can't this be like a 'good' behaviour?
    thanks to this, we could do: $user = User::find(3);
    if( ! $user) echo 'user does not exist'; else {}... not sure here
  • Yes. I agree, in a way this is a good behavior, at the expense of an extra query.
    Is this extra query a issue? I can't say.
    Maybe someone more experienced has something to say about this.

Howdy, Stranger!

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

In this Discussion