Please note that this post refers to CakePHP 3.
Short answer is, as long as you have added both Behaviors in the Table class, you can call a method from one Behavior in another as follows:
class SecondBehavior extends Behavior { public function secondBehaviorMethod() { return $this->_table->firstBehaviorMethod(); } }
I couldn’t find anything about this in the docs, so initially I hoped/presumed that, as long as I had added both Behaviors to my Table class, it was simply a case of calling the method as I would in a Table, so:
In MyTable.php:
public function initialize(array $config) { ... $this->addBehavior('FirstBehavior'); $this->addBehavior('SecondBehavior'); }
In FirstBehavior.php:
class FirstBehavior extends Behavior { public function firstBehaviorMethod() { return "done"; } }
In SecondBehavior.php:
class SecondBehavior extends Behavior { public function secondBehaviourMethod() { return $this->firstBehaviorMethod(); //Gives "Call to undefined method" error } }
However, this just results in a “Call to undefined method” error. However, Behaviors have the $_table property (see the Behavior API) that allows you to access other methods of the current Table. Therefore, just adding _table to the call to firstBehaviorMethod ($this->_table->firstBehaviorMethod()) fixes the issue. As well as accessing methods from other added Behaviors, you can also use find ($this->_table->find(…)), etc.