Yii - 模型和控制器之间的界限 - 什么方法在哪里? MVC原则

发布于 2024-10-20 17:43:16 字数 733 浏览 2 评论 0原文

在 Yii 中,假设我有一个 Employee 表和一个 Company 表。一家公司有许多员工。雇员属于公司。

在创建新员工的表单中,我想提取所有现有公司的列表并将其放入下拉列表中。我知道我可以做到这一点:

$form->dropDownList($model, 'company_id', CHtml::listData(Company::model()->findAll(),'id', 'company')

但是我将在每种形式中拥有很多这样的功能(与不同的模型相关),所以我正在考虑将这种功能放入它们自己的方法中——比如 $model- > getCompanies()。

我的问题是,这种方法最好的地方在哪里?

它应该进入公司模式吗?从视图中,我将访问它:

Company::model()->getCompanies()

它应该进入车站模型吗?这对我来说并没有什么意义,因为这似乎是我会“询问”公司的事情,但我的视图代码将是:

$model->getCompanies()

或者最后,我应该将 getCompanies() 方法放入 Company 模型中,然后调用该方法从 StationsController 的 actionCreate() 中,并将结果发送到视图的 render() ?

从 MVC 角度来看,最合乎逻辑的方法是什么?

in Yii, lets say I have an Employee table, and a Company table. A Company hasMany Emlpoyee. An Emloyee belongsTo a Company.

In the form for creating a new Employee, I want to pull a list of all the companies that exist and put them in a drop down. I know I can do this:

$form->dropDownList($model, 'company_id', CHtml::listData(Company::model()->findAll(),'id', 'company')

But I'm going to have a lot of these in each form (pertaining to different models), so I'm thinking of putting this sort of functionality in their own methods -- something like $model->getCompanies().

My question is, where is the best place for this method?

Should it go in the Company model? The from the view, I would access it as:

Company::model()->getCompanies()

Should it go in the Station model? This doesn't really make sense to me since it seems like something I would 'ask' Company, but then my view code would be:

$model->getCompanies()

Or lastly, should I put the getCompanies() method in the Company model, and then call that method from the actionCreate() of the StationsController, and send the result to the render() for the view?

Whats the most logical way from an MVC perspective?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-10-27 17:43:16

从逻辑上讲,所有这些都记录在公司的活动记录中。
获取公司列表正在使用 Company 表中的数据。
由于活动记录的目的是将与该特定表相关的所有功能分组,因此该功能应该放在其中。

顺便说一句,如果您只想要一个公司列表,则不必创建新功能,只需执行
公司::model()->findAll()

Logically all of that goes in the Company active record.
Fetching a list of companies is working with data in the Company table.
Since the purpose of an active record is to group all functionality related to that specific table, the functionality should go in there.

Btw, if you just want a list of companies you don't have to create a new function, just do a
Company::model()->findAll()

孤独难免 2024-10-27 17:43:16

规则是“瘦控制器,胖模型”。将大部分代码放入模型中。控制器应该简单并路由请求

The rule is "skinny controllers, fat models". Put most of your code in the models. The controllers should be simple and route requests

牵强ㄟ 2024-10-27 17:43:16

看来你做得对。如果您担心一遍又一遍地调用 Company::model()->findAll(),只需在控制器中执行一次 ($companies = Company::model()->findAll()) 并传递 $companies使用 render() 进入视图,就像使用 $model 一样。然后在视图中使用$companies。

我在很大程度上同意“瘦控制器,胖模型”范例,但我在控制器中做了另一件事(除了路由之外):准备在视图中使用的变量。毕竟,这就是您从 $_GET 字符串加载 $model 时所做的事情。所以有时我会准备额外的变量,在本例中是$companies。

It looks like you are doing it right. If you are concerned about calling Company::model()->findAll() over and over, just do it in the controller once ($companies = Company::model()->findAll()) and pass $companies into the view with render(), just like you do with $model. Then use $companies in the view.

I agree with the "skinny controllers, fat models" paradigm for the most part, but I do one additional thing in my controllers (besides routing): prepare variables for use in the view. This is what you doing when you load the $model from the $_GET string after all. So sometimes I will prepare additional variables, in this case $companies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文