views:

19

answers:

2

In my app, I want to add a user notification each time a user receives a comment on an image or other page. Therefore in my add action in my images controller, I'd like to also call the addNotifications action which is in my Notifications controller. I'm trying to stay away from requestAction based on the warnings, but is there another way?

Workflow is this: New event occurs -> trigger addition of notification in notifications table -> email user that notification exists.

A: 

If it's going to be a notification for all sorts of things, then I would consider something in the app_controller as this will make it available across your whole application. Meaning you'll be able to call something like

$this->Notify($user['User']['email'], 'MyNotifyType', 'MyTemplateName');

Then you can deal with the other bits in your app controllers notify function. You might need to add your User model to your app_controller, which could be tricky.

I would try using uses() as this could allow you to add the model and thus pull user data from your app_controller if you wanted to say include the users last login details, username or formal greeting etc. http://api.cakephp.org/class/controller

DavidYell
Thanks! This is what we have decided to go with.
wcolbert
If you have, any chance you could mark it as an accepted answer please? That would be great, and improve your acceptance rate :)
DavidYell
A: 

If you want to call a method that is based on another model, you need to place it in the model class, so in your example in the the Notification model. You can then call it from your Images controller with

$this->Image->Notification->add($params);

if the Models are associated. If they are not, you could either connect them on the fly or go with the previous proposal and add the function in the appController (which is not really perfect, because functions in the AppController should not depend on a certain model but be generic)

harpax