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

Custom Behaviors

The example here:

https://docs.phalcon.io/en/latest/reference/models.html#creating-your-own-behaviors

Says that the $models should be defined as an instance of ModelInterface, I think this is not valid any more for Phalcon version 0.9.1, could you guys check this?

I'm getting this error all the time:

Fatal error: Declaration of Models\Behaviors\Timestampable::notify() must be compatible with that of Phalcon\Mvc\Model\BehaviorInterface::notify() in ..../app/models/Behaviors/Timestampable.php on line 10

Here is the code:

<?php

namespace Models\Behaviors;

use \Phalcon\Mvc\ModelInterface, \Phalcon\Mvc\Model\Behavior, \Phalcon\Mvc\Model\BehaviorInterface;

class Timestampable extends Behavior implements BehaviorInterface {

public function notify($eventType, ModelInterface $model)
{

    var_dump($eventType);die;

    if ($eventType == 'beforeCreate') {
        var_dump($model);die;

        $model->created = date('Y-m-d');
    }
}

}

as soon as I remove ModelInterface type, the error disappear.



32.5k

It's a typo. Look into the https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_BehaviorInterface.html The second argument should be an instance of Phalcon\Mvc\Model\Behavior, not Phalcon\Mvc\ModelInterface.



12.1k

I think you are wrong, the second argument is the model, and \Phalcon\Mvc\Model (https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model.html) do not inherit or implement Phalcon\Mvc\Model\Behavior, maybe the second argument should not have an specific data type, since in my code I'm putting all the models under namespaces.



32.5k

If you implement interface, you must create functions with the same signatures. In your example you implement Phalcon\Mvc\ModelInterface. As you can see, the signature of notify() in Phalcon\Mvc\ModelInterface is:

public notify (string $type, Phalcon\Mvc\Model\Behavior $model)

but yours is

public notify($type, Phalcon\Mvc\ModelInterface $model)

which is not equivalent to needed. And the error says you exactly same as I do.

Docs are written by hand and couldn't be foolproof because of human factor, but API Indice displays actual right structure, because of automatically formed from it. So you should guided by API Indice at first.



12.1k

I get what you say, and I'm agree with you, but that does not work for me.

Here is the code again, and please correct me if the code is still wrong.

<?php

namespace Models\Behaviors;

use \Phalcon\Mvc\Model\Behavior,
    \Phalcon\Mvc\Model\BehaviorInterface;

class Timestampable extends Behavior implements BehaviorInterface
{

    public function notify($eventType, \Phalcon\Mvc\Model\Behavior $model)
    {
        if ($eventType == 'beforeValidation') {
            $model->created = date('Y-m-d');
        }
    }

}

and I still getting the signature error:

Fatal error: Declaration of Models\Behaviors\Timestampable::notify() must be compatible with that of Phalcon\Mvc\Model\BehaviorInterface::notify() in ......./app/models/Behaviors/Timestampable.php on line 9

as soon as I remove the type of $model, everything works.

This is working for me in Phalcon 2.0.8

<?php
use Phalcon\Mvc\ModelInterface;
use Phalcon\Mvc\Model\BehaviorInterface;
use Phalcon\Mvc\Model\Behavior;

class aclBehavior extends Behavior implements BehaviorInterface
{
    /**
     * 
     * @param string $eventType
     * @param Phalcon\Mvc\ModelInterface $model
     * @return mixed
     */
    public function notify($eventType, Phalcon\Mvc\ModelInterface $model)
    {
        if (method_exists($this, $eventType))
        {
            $this->$eventType($model);
        }
    }
}