直接从 MVC 中的控制器使用 ORM 类,不好的做法? [英] Using ORM classes directly from the controller in MVC, bad practice?

查看:10
本文介绍了直接从 MVC 中的控制器使用 ORM 类,不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近深入研究了在我的 CodeIgniter 应用程序中使用 ORM,而我选择的一个是 Propel.现在这使我能够基本上使用 Propels 类作为模型",但这是不好的做法吗?

I've recently delved into using an ORM in my CodeIgniter application and one i've gone for is Propel. Now this gives me the power to basically use Propels classes as the 'Model' but is this bad practive?

所以我的控制器代码如下:

So my controller code would be as follows:

<?php
    class Page extends Controller {
        function __construct() {
            parent::__construct();  
        }   

        function index() {
            $foo = FooQuery::create()->limit(10)->find();
            $data['content'] = array('foo'=>$foo);
            $this->load->view('home', $foo);    
        }
    }
?>

我想在继续开发我的应用程序之前解决这个问题.如果您认为这是不好的做法,请举例说明我应该如何执行此操作.

I want to solve this issue before I carry on developing my application. An example of how I should do this would be very helpful if you do consider this to be bad practice please.

提前致谢

推荐答案

是的,这是不好的做法.

Yes, it's bad practice.

模型应该包含您的所有数据逻辑,并将其从程序的其余部分中抽象出来.对于应用程序的其余部分,模型应该看起来像从中获取数据的黑匣子.如果您使用 ORM 作为模型,您将泄露抽象并将您的控制器紧密耦合到你的数据层.

The model should contain all of your data logic and abstract it all away from the rest of the program. To the rest of the application, the models should look like black boxes out of which it gets its data. If you use an ORM as your model, you're leaking the abstraction and tightly coupling your controller to your data layer.

相反,创建您的模型,并在其中处理 ORM.这样一来,如果您需要调整数据模型,只需在一个地方(模型层)进行更改,就可以知道抽象会成立.

Instead, create your models, and deal with the ORM in there. That way if you ever need to adjust your data model, you can just change it in one place (the model layer) and know that the abstraction will hold.

这篇关于直接从 MVC 中的控制器使用 ORM 类,不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆