Kim Stacks
Kim Stacks

Reputation: 10812

CakePHP: How one Behavior use the Behavior function of a Plugin?

I have two scenarios.

Scenario 1:

A ProcessableBehavior written in my APP/Model/Behavior needs to use a function inside another Behavior called Queryable in a Plugin.

How do I call Queryable.Queryable function doSomething from inside ProcessableBehavior?

Scenario 2:

If I write a Plugin Processable that now contains ProcessableBehavior and so this Behavior depends on Queryable.Queryable function doSomething, how do I do the calling?

Upvotes: 1

Views: 985

Answers (1)

mark
mark

Reputation: 21743

Inside behaviors you can always invoke model methods. Since behaviors attached behave like those, you should be able to call them as they were part of the model.

// behavior 1
public function myFirstBehaviorMethod(Model $Model) {
    // do sth
}

And

// behavior 2
public function mySecondBehaviorMethod(Model $Model) {
     $Model->myFirstBehaviorMethod($foo, $bar);
}

The basic idea is that they don't necessarily have to know about it other. You just assume that they are part of the model (as behaviors enrich the functionality of models).

Note that you don't have to pass the $Model object, as it will internally use $Model.

Make sure you attach/load them in the right order. If one depends on the other you could check on it in setup() etc:

// throw exception if not available
if (!$Model->Behaviors->attached('Queryable') {}

Upvotes: 3

Related Questions