Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
before_save
  • Good day, everyone. I've been wading through documentation, and still haven't found an answer to the following: how do I cancel the $model->save() action as the result of before_save() check? It yii you can do it with setting $event->isValid=false, doing so, the further save() processing is disabled.
  • I've waded through the source code and wondering... if I could just do like
    $this->freeze() in before_save() and voila? But this seems more like a hack - is there a more 'native' way to do so?
  • Throw an exception and catch it specificly if you want to handle it. The way the Observer_Typing or Observer_Validation works is by throwing a specific exception. If for instance I was using both observers I could handle them like this:
    try
    {
        $model->save();
    }
    catch (Orm\ValidationFailed $e)
    {
        // do something with failed validation error message in $e
    }
    catch (Orm\InvalidContentType $e)
    {
        // do something with the wrong type error message in $e
        // (though this really shouldn't happen, you should prevent any type errors before going to production)
    }
    
  • What about the method I described? Will it do?
  • Jelmer Schreuder wrote on Sunday 1st of May 2011:
    Throw an exception and catch it specificly if you want to handle it.

    Maybe you are not getting the question.. What actually bothers me, is to stop the save or delete method from normal processing. For example, if entity has is_locked equal to 1 it can't be deleted, and I need to stop deleting using code only in model. I mean so that if it can't, it just quietly not do it :)
  • Huh, you want it to fail but not give notice of the failure? Throwing an exception will stop the saving or deletion dead in its tracks if it happens before_save/before_delete. And when that happens it should happen clearly with a reason given as to why. EDIT: it would be possible to have it stop saving when false is returned by your method. I think it's bad practice, because nothing should "quietly fail". Make a feature request, or better yet code it up & test it yourself and pull-request. This won't be on top of my priorities list anytime soon, so that's the way to go if you need it fast. EDIT 2: thinking about it, I really don't like it - it'll add a massive amount of "if ($this->observe() == false) return false;" checks for something that is bad practice to begin with. It's not a final decission I'm making here, but warning you that unless someone convinces me I'm not all that positive about the idea.
  • Jelmer, it may be a good idea to look through the Yii code on the subject. I'm sorry I can't point the exact piece at the moment. But I will try to describe it here. you are creating a method like
    before_save($event) {
    //some validation here .. // if it fails i do
    $event->isValid=false; //this thing prevents save() from happening //i'll need to call parent here if this is the model, or do nothing if it is the 'behaviour' thing
    } So maybe we should pass an optional variable to the method, then we should observe it and stop things by isValid parameter or something?
  • Jelmer Schreuder wrote on Sunday 1st of May 2011:
    nothing should "quietly fail".

    Well, i prefer to have a method that makes massive-something, like massive delete, and it should output nothing. Really. Things just get deleted and I see that immediately. If they don't - probably they are locked by some special parameters. If something unexpected happens - php or framework will most probably inform me. So that is why 'quiet-check' is rather important for me. I am closely watching Fuelphp because it is quite simple and doesn't require much php-experience to start working with. For me, it is downgrading from Yii for certain projects. That is why I am trying to reach the same level of clarity with code i plan to do with FP. EDIT:
    I will think about things you've proposed. Maybe I will go for exceptions or propose a commit a code myself. Thanks for the explanation. EDIT 2:
    I have emailed you about documentation translation. Have a look at the mail anytime soon :)
  • I don't really need to look at Yii for this, I know how to implement it in our Orm. Me disliking the idea has little to do with how it is implemented, it is about it being bad practice to work like that.
    Well, i prefer to have a method that makes massive-something, like massive delete, and it should output nothing. Really. Things just get deleted and I see that immediately. If they don't - probably they are locked by some special parameters. If something unexpected happens - php or framework will most probably inform me.

    Orms shouldn't really do massive deletes, that's just incredibly wastefull. Let SQL handle something like that, which is why it's possible to do this through the Orm:
    Model_Example::query()->where('status', '=', 'inactive')->delete();
    

    A much better solution for what you intent, because as I said: normal usage shouldn't fail quietly.
  • I begin to see your point. It's just that I try to apply a yii-way I have already figured out to a CI-derivative framework. That kind of query-constructor usage will suffice for my needs. Thanks.

Howdy, Stranger!

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

In this Discussion