Saturday, November 22, 2014

Re: Events don't work when lazy load model class

Load the class in all requests or have another class acting as an intermediate that would load the required classes when the event is triggered

On Friday, November 21, 2014 11:29:15 PM UTC+1, gonzela2006 wrote:
Hi,

I created an event 'Model.User.created' and attached the listener to my model as you can see below and it is working well

App::uses('AppModel', 'Model');
App::uses('CakeEvent', 'Event');

class User extends AppModel {
   
public function afterSave($created, $options = array()) {
        $event
= new CakeEvent('Model.User.created', $this, array(
           
'id' => $this->id,
           
'data' => $this->data[$this->alias]
       
));
        $this
->getEventManager()->dispatch($event);
   
}
}

/*************************************/
// Attach to model
App::uses('UserListener', 'Event');
App::uses('ClassRegistry', 'Utility');
$user
= ClassRegistry::init('User');
$user
->getEventManager()->attach(new UserListener());

When I lazy load the model class User, the event doesn't work. so How can I allow the event to work on lazy load?
Please advise

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: