Yii Event Does What?

Hey guys,

I’m reading “Yii 1.1 Application Development Cookbook” and i really don’t understand what did you make an event support in Yii? It just does only a simple task as javascript event - eventCall then it does only an explicit action.

If i raise an event "deleteCustomer", i want another module can catch this event.

i.e. I have 3 objects: Student, Teacher, Room.

  • I delete student A, then i raise event deleteStudentA in StudentController.

  • I want to send an email to the Teacher

  • I want to remove his name on the desk of the Room.

But… i don’t want to call sendEmail & removeHisName function in the “region” of Student (StudentController, StudentClass,…) .

I want to catch this event (and do something) in Teacher "region" & Room "region".

How can i do it?


Yii is not such event driven as you figured.

The model events can be cathced only in the model, it means that you can manage the afterDelete only in the function afterDelete of the model.

More generally, a controller exists and can be used only if you are in this controller, so if the user is in an action of this controller.

In short words, don’t write handlers for model event in the controller, write them in the model itself.

Thank you very much, zaccaria!

Okay, so if i raise event deleteStudentA in StudentModel, can i catch this event in TeacherModel, or RoomModel?

No you can’t.

Just don’t think event driven, do like that:

in student:

public function afterDelete()


  // do your stuff here



When you write the afterdelete of the student you can call a function on the related teacher (assuming you have such a relation).

No, i don’t want to do something like that “$this->teacher->doAfterDeleteStudentStuffs()”.

I want to build a flexible app, so i don’t know exactly who will call this event.

Okay, thanks zaccaria! I’m a newbie in Yii, so i think Yii can do it.

Other platform have this feature, so i will research how they do and how Yii do the same.


I am not sure that yii provides such event managment, allowing you to catch an event almost wherever.

The best for you is to use what is in already the framework, and not using feature other platforms has.

Yii is pretty straight forward and easy to learn, but only if you stick on the yii way of doing things.

For the task you described I see as only solution to manage it in the afterDelete of the model itself, of corse you are free to develop an event managment engine, but will be more productive just use the yii one, is already written.