Are events global?

Hi there,

I have a class: User

I would like to add a new event: onNewUser, which I call in User::save when a new instance of User is saved:

public function beforeSave() {

    if ($this->isNewRecord) {

        //raise event


    return parent::beforeSave();


Then, in some place elsewhere, I would like to attach a function to this event. So that when a new User is created, the function will be called.

How do I do that?

Thanks in advance :)

You could find example of implementation here:

Thanks, that’s exactly what I needed. Think this is a limitation to Yii, hopefully it will be implemented some times…

Wow, I got a fatal error about some SQL query as soon as I extended CActiveRecord.

I did not find the actual problem, but with inspiration from Weavora team, I ended up doing:


class BaseActiveRecord extends CActiveRecord {


	private static $_events = array();


	public function init() {


		return parent::init();



	private function attachEvents($events) {

		foreach ($events as $event) {

			if ($event['component'] == get_class($this)) parent::attachEventHandler($event['name'], $event['handler']);




	public function attachGlobalEventHandler($name,$handler) {

		self::$_events[] = array(

			'component' => get_class($this),

			'name' => $name,

			'handler' => $handler




	public function detachGlobalEventHandler($name,$handler) {

		foreach (self::$_events as $index => $event) {

			if ($event['name'] == $name && $event['handler'] == $handler && $event['component'] == get_class($this)) unset(self::$_events[$index]);






It works perfectly to my needs, and now I can both assign handlers as usual, but also do it globally.