Behavior Events: Can't get them to work...

What is the proper syntax for implementing event handling within a behavior?

I've followed the pattern set out in CActiveRecordbehavior (i.e. events() method with corresponding placeholders.

I've tried a variety of approaches. In my tracing through the code, I don't actually see where the events() method is EVER being called. So what is the point if all you have to do is create the methods.

Secondly, I don't see in the CComponent::attachEventHandler() where it looks into the behavior events. It appears to ignore them.

So I guess I'm confused. The documentation on events is ok, but I was misled a few times on how to approach event behaviors.

Can I get some clarification on how to raise events within an attached behavior and have them fire properly?

From what I can tell, you can define events in a behavior, but you cannot raiseEvent() from within the behavior itself. It must be raised outside. Is that true and correct?

This is a problem as I wanted to encapsulate specific functionality within my behavior that raises events to notify the owner object.

What's my best approach to do this?

events() is used in Cbehavior when the behavior is being attached to a class. These events refer to the ones defined in the owner class, not the behavior class.

You should be able to define events in behavior as well. Then your owner needs to attach handlers to the behavior events using:

Got it.

What I ended up doing is taking the event handling out of the behavior and making a new class that attaches the behavior and handles the events. My final class extends that middle-man class.

$owner->asa('behaviorName')->attachEventHandler($eventName,$handler)


  1. What if my $handler is a method in 'behaviorName' class as well as event. How do I call it according to the example above? Something like this?
$owner->asa('behaviorName')->attachEventHandler($eventName, array($owner->asa('behaviorName'), 'methodName'))
  1. Is there a way to attach handlers to event INSIDE behavior class (apart from 'events' method), without need to do $owner->asa('behaviorName')->attachEventHandler… ?

Thanks.