Hi,
I'm actually using afterFetch and beforeSave to set transformation logic on the models and I didn't realize until now that if I create a Model instance and save it the instance retains the beforeSave transformations. For example:
// a model wich receives a datetime object and before save transforms it to datetime string
class Example extends Model
{
public function afterFetch()
{
$this->start = new DateTime($this->start);
}
public function beforeSave()
{
$this->start = $this->start->format("Y-m-d H:i:s");
}
}
so, when I fetch the model I get $instance->start = Datetime object
and for saving:
$example = new Example();
$example->start = new DateTime();
$example->save();
var_dump($example); // $example->start is now a string (used to save in database)
it saves correctly on database but I can't use the object properly since the data integrity fail.
So the question is: How can achieve this? Not using setters and getters manually.
P.D. I already wrote about this on other post etc, but it will be great to have magic getters and setters used when defined.
class Example extends Model
{
public function setStart(DateTime $start)
{
$this->start = $this->start->format("Y-m-d H:i:s");
}
public function getStart()
{
return new DateTime($this->start);
}
}