Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

[SOLVED] beforeSave() model method not working in Phalcon

A friend found out there is what we can refer to as a little bug in Phalcon beforeSave() model method. Even though you override it, it still doesn't work. Let me use an example to describe this


class Car
{
    public function beforeSave()
    {
        $engine = [];

        // Create a Car first Engine
        $engine[0] = new Engine();
        $engine[0]->name = 'Toyota';

        $this->engine = $engine;
    }
}

$car = new Car;
$engine = [];
$engine[0] = new Engine();
$engine[0]->name = 'Mazda';

$car->engine = $engine;
$car->save();



As expected, this is meant to save "Toyota" as the car's engine name but alas, "Mazda" is saved. I would say this is a bug in Phalcon and below is a fix for it.

Have below in the base/parent class of the model or in the model itself.


  public function beforeSave()
  {
       return $this;
  }
  public function afterSave()
  {
       return $this;
  }
  public function save($data = null, $whiteList = null)
  {
       $this->beforeSave();
       parent::save($data, $whiteList);
       $this->afterSave();
  }

 

Just additional, if you would like to debug your save() function not working use call a getMessages() on the object. I mean of you have


$user = new User();
$user->save();
$user->getMessages();

This works well. I guess it solves someone's problem.

 


comments powered by Disqus