CodeIgniter:什么是好的模型

发布于 2024-09-07 04:20:35 字数 329 浏览 11 评论 0原文

CodeIgniter 中良好的 MVC 模型的要素是什么。我的“用户”模型现在所做的基本上是使用数据库中相同的活动记录函数。唯一的区别是,您不需要指定数据库表,只需这样做:

$this->usermodel->where('username','test'),
$user = $this->usermodel->get();

这感觉有点尴尬,因为它并没有使它“变得更容易”。

我想到的另一种方法是使用户模型像具有加载功能的用户对象一样。但当加载超过 1 个用户时,这效率不高。

我可以从你们那里得到一些建议吗?谢谢。

What makes a good MVC model in CodeIgniter. What my 'user' model does now is basically using the same active record functions from the database library. The only difference is that you don't need to specify the database table and just do:

$this->usermodel->where('username','test'),
$user = $this->usermodel->get();

This feels kinda awkward, since its not making it 'a lot easier'.

Another way I thought of was making the user model like an user object with a load function. But this is not efficient when loading more than 1 user.

Can I get some tips from you guys? Thanks.

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

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

发布评论

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

评论(3

遗弃M 2024-09-14 04:20:35

一个好的提示是,不要让模型视图彼此直接对话,您总是(或在可能的情况下)必须使用控制器来进行这种通信。

我还听说过这样的观点,即在模型中包含逻辑是一种不好的做法...我不知道这是否属实,但这是我多次违反的规则(如果有人更了解该主题,请更正)我)。

当然,始终牢记模型应该是可重用的,因此我尝试为某些问题提供通用解决方案,而不是特定于应用程序......另一方面,控制器似乎是一次性组件,因此这是您应该做丑陋事情的地方永远不会在其他项目中使用......

希望它有一点帮助

A good tip would be, not letting model an view talk with each other straight, you always(or when it's possible) have to use controller to do this kind of communication.

I've heard also the opinion that it's a bad practice to have logic inside your models...I don't know if this is true but it's a rule I have broken lots of times(if somebody knows more about the subject please correct me).

And of course always have in mind that model should be reusable , so I try to give general solution to some problems and not application specific...on the other hand controller seems to be the throwaway component so this is where you should do ugly stuff that will never be used in other projects...

Hope it helps a little

违心° 2024-09-14 04:20:35

我认为当您处理大量数据并且需要进行许多复杂的查询时,就会出现“容易得多”的情况。据我了解,如果您正在处理相对简单的事情,您可以放弃模型并从控制器内进行数据库调用。本身并没有“最佳实践”,而是个人偏好。

MVC 架构允许更好地划分代码,并且可以允许更好的结构和重用代码,但是您不需要严格遵循 MVC 理念来完成许多事情——同样,这取决于偏好。

I think 'a lot easier' comes in when you are working with large sets of data, and have many complex queries to make. As I understand it, if you are working on something that is relatively simple, you can forego the model and make your db calls from within the controller. There isn't a 'best practice' per se, but rather personal preference.

The MVC architecture allows for better compartmentalizing of code, and can allow for better structure and re-use of code, but you needn't follow the MVC idea to the letter to accomplish many things -- again it comes down to preference.

绝影如岚 2024-09-14 04:20:35

重点是将所有应用程序逻辑抽象为模型,并使用控制器来“控制”您的 Web 界面并在 Web 界面和模型之间进行中介。

这些模型是您的程序本身,您应该能够非常轻松地完全重新设计用户界面,而不影响应用程序模型。

The point is to abstract all of your application logic into Models and use Controllers simply for 'controlling' your web interface and mediating between the web interface and the models.

The models are your program proper you should be able to pretty easily completely redesign the user interface without affecting the application models.

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