1

I got stucked in a problem. hope anyone 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.

Aakash Sahai
  • 3,935
  • 7
  • 27
  • 41

1 Answers1

3

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.

Ross
  • 18,117
  • 7
  • 44
  • 64
  • Ross i think u didnt fynd that i m trying to use only single model that means i m not going to use any specific function/method of model class.i m just askg how can i retrive data from controller in model's 'constructor'. and i think u didnt undrstand MVC correctly.because according to MVC we 'should' validate data in model, not in controller. controller is only for directing things.not manipulating things – Aakash Sahai May 06 '11 at 10:58
  • Some would [beg to differ](http://stackoverflow.com/questions/5651175/mvc-question-should-i-put-form-validation-rules-in-the-controller-or-model). As your requirements aren't entirely clear, validating in the controller *is* acceptable - but maybe not for your application. – Ross May 06 '11 at 12:27