2

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.

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

example models/users.php:

class user extends IgnitedRecord {

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

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

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

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 ,

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

** 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 !!!??

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.

example of problem:

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
?>

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 !!

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 !

4

1 に答える 1

1

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.

于 2013-03-25T19:52:04.657 に答える