开发者

CI - Passing values from controller to model and accessing them in model constructor

I got stucked in a problem. hope any开发者_运维知识库one will help it out. i m using codeigniter to build my application. Now i have made an idea to create a single model for whole application. But how is it possible??my senior also asked me this, and told me itz not possible. but i m very close to success. only thing lacking is passing data from controller to model's constructor.

Actually my idea behind it is : -> i will send all data to model's constructor(if possible)

->then validating data(using my own validating class)

->setting class properties dynamically(using standard class)

->executing query(query type will reside in data array passed through controller).

suppose array[0] stores 'insert' so i will execute query using : $this->array[0]->.. etc

but my 1step is big problem to me..as i cant say that further things will work or not.


I don't get what you're asking really, and it looks like you plan on writing your own code for a lot of stuff the CI framework already does for you.

Here is an example how to pass data from controller to your model:

class Foo extends CI_Controller {

    function bar()
    {
        $data=array('foo'=>'bar', 'abc'=> 'def');
        $this->load->model('my_model');
        $this->my_model->do_something($data);
    }
}

class My_model extends CI_Model {

    function do_something($data=array())
    {
        print_r($data);
    }
}

You can write your own model functions for insert:

function insert_data($table, $data=array()) {
    $this->db->insert($table, $data);
    // etc
}

but that's just active record - functionality that already exists.

You can pass the data to the models constructor if necessary, yes. Sounds like you should rethink your approach a little however.

Validation can be done in the controller (and indeed, the model), using CI's validation class which can be extended as required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜