1

我有两种情况。

场景一:

在我的 APP/Model/Behavior 中编写的 ProcessableBehavior 需要在插件中另一个名为 Queryable 的 Behavior 中使用一个函数。

如何从 ProcessableBehavior 内部调用 Queryable.Queryable 函数 doSomething?

场景二:

如果我编写了一个现在包含 ProcessableBehavior 的 Plugin Processable 并且此 Behavior 取决于 Queryable.Queryable 函数 doSomething,我该如何进行调用?

4

1 回答 1

3

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') {}
于 2013-02-19T11:55:18.937 回答