简体   繁体   中英

Actions to be performed in controller or in model?

I've got a table of items, and each item can be assigned given number of images, some of them are featured images. Just one of these must be the enabled at a time.

I have these fields in the images table: featured (0 or 1) enabled (0 or 1)

Following the MVC architecture, I'm not sure whether I should do the following in the controller or in the model when for instance I enable an image (I've simplified the example since it's a little more complex):

  1. Check if this image is featured.
  2. In case it is featured, disable any featured image and enable this one.

I could either get its state (featured or not) from the model to the controller, and let the controller order the model to disable all the featured images and next tell the model to enable this one, or let the model manage this all internally.

My question came up because as far as I know and I've sometimes read, the controller is said to be the engine while the model simply does what the controller orders.

However I thought in this case the model might be self-reliant to manage these constraints as a black box without needing that the controller is aware of them.

What do you think?

I would suggest you to increase a field in your items table. And store the id of the enabled image(that can be featured or enabled image).

This method will resolve your both issues

1.Check if this image is featured.
2.In case it is featured, disable any featured image and enable this one.

Everytime, just store the id of that image which is enabled or which is featured. So, you don't have to make much updations in images table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM