Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Call to undefined behavior method from repository

I have this model

namespace Project\Models;

use Phalcon\Mvc\Model;
use Project\Behaviors\xBehvior;
class X extends Model
{
    // ... opted-out code
    public method initialize()
    {
        $this-addBehavior(new xBehavior());
    }
    // ... opted-out code
}

And a behavior

namepsace Project\Behaviors;

use Phalcon\Mvc\Model\Behavior;
use Phalcon\Mvc\Model\BehaviorInterface;
class xBehavior extends Behavior implements BehaviorInterface
{
    // ... opted-out code
    public function any()
    {
        return ['val1', 'val2', 'val3'];
    }
    // ... opted-out code
}

And a repository

namespace Project\Repositories;

use Project\Models\X;
class xRepository extends X
{
    public function doOperation()
    {
        $result = self::find();
        return $result->any();
    }
}

When I call ($xRepository->doOperation()) it should return ['val1', 'val2', 'val3'], but it throws an exception that method any() is undefined. Do I did something wrong ?

any() is part of the behavior and you call it from the model. You can fire an event with fireEvent('eventName')

namepsace Project\Behaviors;

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

class xBehavior extends Behavior implements BehaviorInterface
{
    public function notify($eventName, ModelInterface $x)
    {
        if ($eventName === 'superEvent') {
            return ['val1', 'val2', 'val3'];
        }
    }    
}

and then in your repositorie

namespace Project\Repositories;

use Project\Models\X;
class xRepository extends X
{
    public function doOperation()
    {
        $result = self::find();
        return $result->fireEvent('superEvent');
    }
}

Good luck

Thanks alot ! But how could "Nestedset" behavior in Phalcon incubator project do this ?

You can call "descendants, ancestors, ...etc" directly from the model. Check this https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/Model/Behavior

any() is part of the behavior and you call it from the model. You can fire an event with fireEvent('eventName')

namepsace Project\Behaviors;

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

class xBehavior extends Behavior implements BehaviorInterface
{
   public function notify($eventName, ModelInterface $x)
   {
      if ($eventName === 'superEvent') {
          return ['val1', 'val2', 'val3'];
      }
   }    
}

and then in your repositorie

namespace Project\Repositories;

use Project\Models\X;
class xRepository extends X
{
   public function doOperation()
   {
       $result = self::find();
       return $result->fireEvent('superEvent');
   }
}

Good luck



1.5k
Accepted
answer

I found that if you want to call a behavior method from model, you should use ($this->behaviorMethod()) way since the behavior is initialized and binded to the model whenever you request it.

sure that is an other way. more info