Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
pre_save and pre_validate
  • I need to set a model property before validate/save and I have tried tu use pre_save and pre_validate on my model without success. they never get called.
    how does this work? Is this the best approach?

    class Model_AppPageView extends Model_BaseModel
    {
    (...)

    public function pre_validate($data)
    {
    $data->idRegistrant = Session::get('idRegistrant', null);
    return $data;
    }
    }
  • What kind of model is this? i.e. what is Model_BaseModel ?
  • My BaseModel extends \Orm\Model and pre_save only work for classes that extends \Crud\Model right?
    Is there a way to have a before_save / before_validate in my model class rather than in a observer.
    With the observer i have to create a class to have 1 line of code :P

    another thing. I tried the observer aproach with before_save and still does not pass validation. The field idRegistrant is mandatory. It only worked with the after_create event? is this normal?





    class Observer_Registrant extends \Orm\Observer
    {
    public function after_create(Orm\Model $model)
    {

    if(strtolower(get_class($model)) != "model_registrant" && empty($model->idRegistrant)){
    $model->idRegistrant = Session::get('idRegistrant', null);
    }
    }
    }


    thanks
  • HarroHarro
    Accepted Answer
    Correct. The ORM works with observers, which is more event driven.

    You can not have the observer methods in your model, any observer needs to extend \Orm\Observer, it needs the methods from the base class in order to do it's job.

    You say "it does not pass validation". What validation?
  • Thanks for your awnser. And since its my first post. Great job with Fuel.

    I use Observer pattern for when the code is usefull for multiple Models but in this case...
    I could have this on my model and base model. So much simpler and beautiful for code that belongs only to the model in question :)
    Is there any drawbacks with this approach that i am missing?

    MODEL
    public function save($cascade = null, $use_transaction = false){
    // My code here
    return parent::save($cascade, $use_transaction);
    }
    BASE MODEL
    public function save($cascade = null, $use_transaction = false){
    return parent::save($cascade, $use_transaction);
    }
  • Thanks.

    Since this is about ensuring a (correct) property value, the most logical place to do this is when validating your input. Which is why I asked how you validate.
  • i just call save and catch validation errors so i think this works nice.
  • Hmm... If it works for you. I'm not really a fan of overloading methods, if it's not really needed.

    It would be more logical to have a custom validation rule (instead of "Required"), that would fill in the field from the session as a default value if it isn't set.

Howdy, Stranger!

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

In this Discussion