问题
I'm writing a plugin which includes a behavior that has a dependency on the ContainableBehavior
. In my behavior, I'd like to tweak any query conditions in its beforeFind()
callback, but I'm finding that ContainableBehavior::beforeFind()
has already been executed so my changes of course are falling on deaf ears, so to speak.
The only solution, as far as I can tell, is to manually change the behavior execution order so that my behavior's beforeFind()
method is called before ContainableBehavior::beforeFind()
, but I'm having some trouble making that happen.
I don't want to make any assumptions about the apps that may use my plugin so I don't want to create an arbitrary dependency that defines how users have to configure any behaviors they're using. I'd like to just make the necessary adjustment on the fly where I need to make it. What I thought made the most sense is this:
In MyBehavior::setup()
, I'm simply trying to detach and reattach the ContainableBehavior
so that in the collection of behaviors attached to the parent model, it falls after MyBehavior
:
# Assume that a condition checking existence is in place
$model->Behaviors->detach( 'Containable' );
$model->Behaviors->attach( 'Containable' );
If I then dump the list of attached behaviors, I get an array with the attached behaviors ordered the way I want them, but ContainableBehavior::beforeFind()
still fires before MyBehavior::beforeFind()
.
Is there any way to force the execution order of behavior callbacks that isn't a total hack job or that doesn't impose an unrealistic dependency/standard on any apps that may deploy the plugin?
Thanks.
来源:https://stackoverflow.com/questions/10421701/reordering-behaviors-in-cakephp