1

I want to ensure that any attempt to set a date attribute on a model by way of populateModel() or myModel->foo = 'bar' (basically any method of setting a value) is intercepted so I can set a default if the value isn't valid.

Put another way, if you attempt to set a date prior to today (or a string that's not a date), I want to set the attribute to be today's date.

Currently I'm doing that in my populateModel() method but that doesn't catch the setAttribute('foo') approach or the myModel->foo approach.

For example in Laravel, we can create a setter method: setFooAttribute() so I was hoping for something similar.

2

In your model you can override setAttribute(), which covers calling setAttribute('foo', 'bar') directly as well as $model->foo = 'bar'.

public function setAttribute($name, $value)
{
    if ($name == 'foo')
    {
        // Not sure if your value will be a dateTime object, string, etc.
        // But check to see if it's < than today's current date.  If so,
        // set $value = new DateTime();
    }

    parent::setAttribute($name, $value)
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.