We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Overwriting Methods in \Phalcon\Mvc\Model

We recently came to a bridge in our application development, where we need to update a pivot table. Unfortunately, we realized that update()/save() will create a new row for us each time because of the fact that there is no index on our pivot table's model. My first thought was to overwrite the update() method and create my own. Unfortunately, this does not work and just dies out.

My initial thought was to simply overwrite the method, with nothing aside from the extending that is already happening on the model. That didn't work. I thought implementing the appropriate interface would help, but that didn't work either.

My question is, based on this information, is there a way to overwrite methods of a base class in a new class? If so, any help would be appreciated. I haven't been able to find anything in the docs, regarding overwriting models.

Thanks in advance!



98.9k

You can override any method in Phalcon by just extending the class and implementing the method with the same parameters:

<?php

class Robots extends Phalcon\Mvc\Model
{
   public function save($data=null, $whiteList=null)
   {       
       return parent::save($data, $whiteList);
   }
}


5.1k

That would do it. I forgot to specify the arguments.

Thanks again!