Overriding the save()-method of \Phalcon\Mvc\Model fails on related models


I'm having a set of related models in my project and want to override the save()-method of "\Phalcon\Mvc\Model" to implement some logging and exception handling, so I don't have to check for errors on every single save()-call.

To accomplish this, I created a "Basemodel"-class which extends "\Phalcon\Mvc\Model" and have all my models extend "Basemodel". In "Basemodel" I included a save()-class, which (for starters) only looks like this:

public function save($data = null, $whiteList = null)
    parent::save($data = null, $whiteList = null);

This works when using it on models that are not related to other models but will fail on a model that's connected to other models (using a relation-table and a many-to-many-relation). The strange thing is that it works when I remove the save()-method in "Basemodel", so saving a model with relation does actually work, just not when using parent::save() - why would it make any difference to use an overwritten method that just calls "parent::save()"?

Any hint is appreciated. Kind regards, Dirk

HI Maybe, I had got same issue https://github.com/phalcon/cphalcon/issues/1296 Can you use postSave hook on another for this task without overwrite save? See http://docs.phalconphp.com/ru/latest/api/Phalcon%5Mvc%5Model.html

The best way if you can submit simple test case for it to github issue tracker with full description https://github.com/phalcon/cphalcon/issues/new But I am afraid that you have got a bug in your code, not phalcon


thanks for your answer. I've been trying to solve the problem with the postSave-hook, so I checked \Phalcon\Mvc\Model and found this: protected function _postSave(){ }

So according to the documentation, saving a model should result in my "postSave"-method to be called. Unfortunately this doesn't work, my method is never called. After hours of debugging and reading the documentation, I found out why: The PHP-code from the developer tools (current version downloaded here: https://github.com/phalcon/phalcon-devtools) differs from the documentation found here: http://docs.phalconphp.com/en/latest/reference/models.html#events-and-events-manager - where the method to use is called "afterSave" (and not "postSave").

Now I'm using the "afterSave"-hook and everything works like a charm.

This is, unfortunately, not the first time where the online-documentation and the code from the developer-tools differ, so anyone running into similar problems should try things from both sources before questioning their abilities ...

Kind regards, Dirk