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

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

问题描述

我最近深入我的CodeIgniter应用程序中使用了一个ORM,而我去之一是Propel。现在这让我有权力将Propels类作为模型,但这是不好的做法吗?



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

 <?php 
类页面扩展Controller {
function __construct(){
parent :: __ construct ();
}

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

在进行开发应用程序之前,我想解决这个问题。如果您认为这是不好的做法,我应该如何做这个例子将是非常有帮助的。



提前感谢

解决方案

是的,这是不好的做法。



该模型应该包含所有的数据逻辑,并将其全部抽象远离程序的其余部分。对于应用程序的其余部分,模型应该看起来像黑盒子,它从中获取其数据。如果您使用ORM作为您的型号,则泄漏抽象并将控制器紧密连接到你的数据层。



相反,创建你的模型,并处理那里的ORM。这样,如果您需要调整数据模型,您可以在一个地方(模型层)更改它,并知道抽象将成立。


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.

Thanks in advance

解决方案

Yes, it's bad practice.

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.

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天全站免登陆