Yii Framework Forum: Where, How And When With Events - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Where, How And When With Events Trying to understanding events Rate Topic: -----

#1 User is offline   Dean Sanderson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 18-July 12

Posted 08 January 2013 - 06:53 AM

Hi Guys,
I am very confused about this events feature in Yii. I understand its similar to Drupal’s Hook system. In a sense i can call different sections of code when an event occurs. EG i update a product in a database, then I tell my application that onProductUpdate has happened. Then from different modules and other areas of my code i could call different functions that can add to this.
I just cant seem to get my head around getting it working and where to put the code. The only way i can seem to get it working is if i have the event call handlers in the innit function where the event is raised, i don’t see how this can benefit if somebody else creates an add-on module they would have to edit the original init function.
I must be reading something wrong here! I have read books and almost everything online! There doesn't seem to be a very good tutorial to get me started!

My situation is as follows:

I have a products, when this product has been sent I raise an event. From there in the future when I add a stock system to the app I want to just add a module that changes the stock level when this event occurs without editing the original code.

Thanks in advanced and I hope someone can help!
0

#2 User is offline   anesed 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 01-June 11

Posted 28 January 2013 - 02:52 PM

Shameless Bump.

I don't get what's the deal with Yii's events, I feel like it's just easier to call the function directly since they won't respond to the event without me explicitly registering them. Is there a way to make it work like Drupal's hook system?.

[Edit]
I'm still not convinced by the event system, probably the only thing I don't like about Yii. I read this article and thought "am I doing this wrong?", so I decided to try more or less what I saw, trying to handle a module's event from another module, so I did the following:
In my ApiModule, y added
public function onVainaCheck(CEvent $event) {
    $this->raiseEvent('onVainaCheck', $event);
}

In my GestModule's init(), I added this line
Yii::app()->getModule('api')->onVainaCheck=array($this, 'check');

GestModule has a public method "check" that prints "!" and nothing else.
In my ActController's actionCommit, inside ApiModule, I added the line
$this->module->onVainaCheck(new CEvent(array('x'=>'y')))

Ran /api/act/commit and nothing happened, my guess is GestModule::init() was never called and so the handler was never attached, which makes sense; but then, other modules couldn't handle the onVainaCheck event unless they are initialized before this happens, i.e. if I add a functionality and try to extend it by responding to an event in another module, I can't do it without altering said functionality's code. Could I, at least, do this in the config file or somewhere with a scope broad enough for me not to worry about changing my code everytime I add a module that has to respond to an event?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users