Welcome Guest, Not a member yet? Register   Sign In
Creating multiple instances of a model
#1

(This post was last modified: 04-28-2016, 07:54 AM by CINewb.)

Hi there

I am wondering, how do you create multiple instances of a model?

For example:


PHP Code:
foreach ($query->result_array() as $row) {
    $this->load->model('customer_model');
    $this->customer_model->setName($row['name']);
    $customers[] = $this;



With the above example, what I end up with is every customer having the same name because each element of the $customers array is referencing the same instance.

The way I've figured out to do it is:


PHP Code:
$this->load->model('customer_model');
foreach (
$query->result_array() as $row) {
    $c = new Customer_model();
    $c->setName($row['name']);
    $customers[] = $c;



Is the second example acceptable, or does it break CodeIgniter best practice?

Thank you
Reply


Messages In This Thread
Creating multiple instances of a model - by CINewb - 04-28-2016, 06:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB