简体   繁体   English

在控制器或模型中要执行的动作?

[英]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) 我在图像表中具有以下字段:启用(0或1)精选(0或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): 遵循MVC架构,例如,当我启用图像时,我不确定是否应该在控制器或模型中执行以下操作(我简化了示例,因为它稍微复杂一些):

  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). 并存储已启用图片(可以是精选图片或已启用图片)的ID。

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. 每次,只需存储已启用或具有特色的图像的ID。 So, you don't have to make much updations in images table. 因此,您不必在images表中进行太多更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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