Good day to all !

There is a certain task for which I can not choose the right way ( perfectionism hurts :) )

There are models:



Plan has a relation of Invoice plan.invoice_id = invoice.id

Needs the following:

When saving Invoices notify Plans ( ie invoice has been paid, need to activate the plan .)

Use existing Events in Yii ( as shown in most of the examples of how to use them ) do not see a possibility , because in invoice save action plan model not present and therefore can not attach event to it.

The idea of ​​implementation:

Make a mechanism of global hooks.

Creating a component (Hooks) which preloads on application init and reads , let it be protected / config / hooks.php.

In this file we’re registering a hook, for example:

// protected/config/hooks.php

Yii::app()->hooks->onInvoicePaid('Plan', 'onInvoicePaidHandler');

Then Invoice, fires event onInvoicePaid.

I really can put calling of Plan model into afterSave of Invoice, but I think it’s not clear way, because invoice can be paid not just for plan (many other items).

So I want each separate item handle invoice payment and think what to do with it.

Maybe you have some ideas (how to resolve this issue without global hooks , how would you do it)?

You can save a lot of time by doing it a simple way (in afterSave() method) :)

You can make it look clear by adding special methods to all items:

protected function afterSave()



    if (/* Plan */)


    else if (/* Another item */)



Or if there are really a lot of items:

protected function afterSave()



    $this->getRelatedItem()->onInvoicePaid(); // all possible related items must have this method