4

I am currently adding a notification feature to a website and need it to update it's data whenever a user is added to a table. The way I think is correct is to go through each of my controllers and whenever a call to the model is made that will change that table, I will follow that up with a second function call updateNotificationTable() or something like that.

The site is quite large however, and while I can make sure to follow all of these controller calls with an update function, I can easily see this being missed for any future changes that interact with the table.

It seems the most fool proof way to do this would be to include a function inside the model itself so that once it inserts or deletes a row from the table it would update the notifications there. The problem is that the notification process pulls data from a separate second model I would have to load up inside the starting model.

I don't believe it's supposed to work that way but the alternative would present problems too. In this instance would it be right to load another model inside my model to process the notification update?

edit: sample code

function addToTable(){
    //There's probably a dozen or more functions like this scattered throughout various controllers, they all need to be followed by updateNotifications()
    $this->other_model->Insert_stuff();
    $this->updateNotifications();
}

function updateNotifications(){
    $var1 = $this->model1->get_important_info();
    $var2 = $this->model2->get_more();
    $this->Notifications_Model->doTheUpdate($var1,$var2);
}

If I could instead take doTheUpdate() and have it directly call model1 and model2 to get it's required data then any future function that need to call $this->other_model->Insert_stuff(); will not need a follow up updateNotifications() call. If another programmer is working on it chances are good it won't happen.

There's probably a dozen or so controllers that I add this function to and call after various functions are executed. If Notifications_Model could directly call model1 and model2 then doTheUpdate

user3653863
  • 227
  • 3
  • 13
  • Is there any code you could show to explain what you are doing? Something simple would help – nerdlyist Apr 11 '18 at 17:03
  • @nerdlyist Sure, I've added it to my post – user3653863 Apr 11 '18 at 17:28
  • Where is the persist happening that would trigger this notification? In `Insert_stuff()`? I would personally put the notification there if possible. – nerdlyist Apr 11 '18 at 19:50
  • @nerdlyist so it would be ok to load my required models in 'other_model->Insert_stuff();' to gather the required data? – user3653863 Apr 11 '18 at 20:36
  • Read [this](https://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000) and watch [this](https://www.youtube.com/watch?v=dYvSaajboEs) (at 15:39). Then, in each of your controllers receive just a `UserAndNotificationsService`. This receives `UserService` and `NotificationsService` as constructor args and a method `updateUserAndNotifications`, which does all the updates (user+notifications) by using the two services. – PajuranCodes Apr 12 '18 at 02:38
  • These two services receive corresponding data mappers in their constructors and define corresponding methods (`update`, `getImportantInfo`, etc) with corresponding domain objects as arguments. – PajuranCodes Apr 12 '18 at 02:38

0 Answers0