Welcome Guest, Not a member yet? Register   Sign In
Multiple Instances of a Library?
#1

[eluser]jkarlen[/eluser]
I'm working on an order management system and I'm trying to load an array of OrderProduct items within my Order object. However, it appears that the load method is only firing once as if I look at an order with many items on it the result is a list of all identical items.

I appologize if this is a novice question - but when it comes to code igniter that's what I am.

function get_products()
{
$query = $this->CI->db->get_where('xxx.order_product', array('order_id' => $this->order->id));
foreach($query->result() as $row)
{
$this->CI->load->library('WorkroomOrderProduct', array('row' => $row));
$p = $this->CI->workroomorderproduct;
$this->products[$p->get_product_name()][$p->get_material()->fabric][$p->get_material()->color][] = $p;
}
return $this->products;
}

The resulting array contains all identical objects though each $row iteration does have a correctly unique value.

Can I use the optional 3rd parameter of load() to pass a variable by reference thereby assigning it to that? Just naming the variable as the documentation says doesn't help - I still need a new "blank" variable each time to assign to which is why I'm thinking doing it by reference might help.
#2

[eluser]TheFuzzy0ne[/eluser]
If you want a new instance of a class, you need to instantiate it with the new keyword.

Code:
function get_products()
  {
      $query = $this->CI->db->get_where(‘xxx.order_product’, array(‘order_id’ => $this->order->id));
      foreach($query->result() as $row)
      {
        $p = new WorkroomOrderProduct(array(‘row’ => $row));
        $this->products[$p->get_product_name()][$p->get_material()->fabric][$p->get_material()->color][] = $p;
      }
      return $this->products;
  }

But you'll need to load/include the file first.
#3

[eluser]jkarlen[/eluser]
Or, I did this:

foreach($query->result() as $row)
{
$key = 'product_' . $row->id;
$this->CI->load->library('WorkroomOrderProduct', array('row' => $row), $key);
$p = $this->CI->$key;
$this->products[$p->get_product_name()][$p->get_material()->fabric][$p->get_material()->color][] = $p;


Looking at your example...if I call $this->CI->load->library('WorkroomOrderProduct') can I then call new WorkroomOrderProduct without using CI or do I still need to include the class file in old-fashioned PHP style?
#4

[eluser]TheFuzzy0ne[/eluser]
You can do one or the other - whichever you prefer. I'd suggest just including it if it's meant to be an object with multiple instantiations. Or, you could add a method to your library that will return a newly instantiate version of itself.




Theme © iAndrew 2016 - Forum software by © MyBB