Codeigniter MVC,我应该把我的业务逻辑从模型转移到控制器使用ORM? [英] Codeigniter MVC, should i move my business logic from model into Controller to use ORM?

查看:141
本文介绍了Codeigniter MVC,我应该把我的业务逻辑从模型转移到控制器使用ORM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我目前的专案从使用常规的CI查询生成器使用ORM,因为我的数据库越来越大,我有更多的20 FK和表之间的关系。

Im trying to migrate my current project from using regular CI query builder to use an ORM since my database is growing larger and i have more that 20 FK and relation between tables.

所以我目前正在寻找一个不错的orm的CI。我发现IgnitedRecord,但我面临一个巨大的问题。

so i'm currently searching for a nice orm for CI. i found IgnitedRecord but i faced a huge problem.

示例models / users.php:

example models/users.php:

class user extends IgnitedRecord {

public function is_loged(){
return $this->session->userdata('user');
}

问题是 $ this 不再指向CI,它返回未定义的属性:user :: $ session 与任何CI类/库相同。

Problem is that $this is not pointing to CI anymore, and it return Undefined property: user::$session same with any CI class/library.

所以有办法解决这个问题吗?或者我需要使用 $ CI =& get_instance(); 在每个i.r.

so is there a way to fix that ? or do I need to use $CI =& get_instance(); inside each i.r. model ?

由于我的模型包含我的大部分应用程序业务逻辑,我觉得这有点不舒服 $ CI =& get_instance(); 在所有我的模型,

As my models contain most of my app business logic, I find it a bit uncomfortable to do $CI =& get_instance(); in all my models ,

其他解决方案是将所有逻辑移动到控制器! OR垃圾桶这个orm,并坚持使用原生的查询生成器:p。

other solution would be to move all logic into controller ! OR trash this orm and stick with native query builder :p.

** SO next问题**当使用orm's(我从来没有使用过)我的逻辑到控制器,只保留模型的orm !!!

** SO next question** When using orm's (I have never used one before between) should I move my logic to controllers and only keep models for the orm !!!??

会破坏我的DRY法力,因为很多时候我将属性/方法设置为模型,并从视图/其他控制器访问它们无法在内部完成的功能

that will destroy my D.R.Y mana, because a lots of time I set properties/methods into a model and access them from views/other controllers a feature that cannot be done inside controllers as I cannot reference a controller property/methode from views/other controller.

问题示例:

class Auth extends CI_Controller{
public $msg='hello world';

function index(){
$this->load->view('login');
}
}


class user extends CI_MODEL{
public $msg='HELLO FROM YOUR MODEL';
}

/*VIEW */
<?
echo $this->user->msg;//WORKS fine.
echo $this->auth->msg;//WILL NOT WORK. will throw an UNIDENTIFIED auth error
?>

同样,我设计的流程通常是控制器从加载所有插件开始(例如:models / plugins / * .php)这些插件设置一些数据属性供主模型使用,加载所有插件控制器加载主模型并退出其逻辑并将结果发送回查看:)

Also way i designed my flow usually that controller start by loading all plugins(eg: models/plugins/*.php ) these plugins set some data properties for the main model to utilize, after loading all plugins controller loads the main model and excute its logic and send result back to view :).

example

models/plugins/settings.php:
    function config(){
        $this->msg = 'hello';
        $this->user = 'sam';
        return;
    }

models/hello.php
    function replace_sam (){
        $new='NONAME';
        if($this->settings->user=='sam') return $this->settings->msg.' '.$new
        else return $this->settings->msg.' '.$this->settings->user;
    }

controllers/home.php
    function index(){
        $this->load->model('plugins/settings');
        $this->settings->config();

        $this->load->model('hello');   

        echo $this->hello->replace_sam();
        //echo hello NONAME
    }

从这个简单的例子..所以你认为我应该重写所有的东西,并将逻辑移动到控制器!

well i hope u got my point from this simple example.. so do you think i should rewrite every thing and move logic to controller !!

我感谢你的意见。所以如果你认为我应该离开模型为orm,那么请解释一下,这不会是一个巨大的浪费!

i appreciate your opinions. so if you think yes i should leave model for orm, then please explain to me how that would not be a huge waste !

推荐答案

如果你看看system / core / model.php,你会看到它是一个非常简单的类。它所做的就是调用 $ CI =& get_instance()。所以在你的类中加入相同的 __ get()方法会使它们与内置的一样好。

If you look at system/core/model.php, you will see that it is a very simple class. All it does is call $CI =& get_instance() when needed. So adding that same __get() method to your class will make them every bit as good as the built-in ones.

这篇关于Codeigniter MVC,我应该把我的业务逻辑从模型转移到控制器使用ORM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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