CodeIgniter Forums

Full Version: Load more than one instance of the same library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mattalexx[/eluser]
How would I load more than one instance of the same library?

I have this:

Code:
$this->load->library('SelectQuery', array('table' => 'items'));
$this->selectquery->addGroupBys("items.id");
$this->selectquery->addJoins("

   # Must have categories with images and a parent
   JOIN categories_items
      ON items.id = categories_items.item_id
   JOIN categories
      ON (
         categories_items.category_id = categories.id
         AND categories.in_nav != 0
         )

   # Must have an artist
   JOIN artists
      ON items.artist_id = artists.id

   # Must have at least one image
   JOIN item_images
      ON item_images.item_id = items.id
   ");
$this->selectquery->addWheres("
   items.show != 0
   AND items.quantity > 0
   ");
$this->selectquery->addOrderBys("
   items.title ASC,
   items.id ASC
   ");
$this->selectquery->addFields("
   CEILING(100 - items.sale_price/items.price*100) AS percent_off
   ");

It seems like once the $this->selectquery variable is used once, it can't be used again without being overwritten.

El Forum

[eluser]Pascal Kriete[/eluser]
CI includes the class, does some stuff, instantiates it, and stores that object reference in an array. What you get when you say $this->load is that reference. That's done so that you don't actually overwrite the instance when you use it in say a controller and later on in another library.

The easiest thing to do is to just make a new one. Remember, it's already included so all you do is:
Code:
$bla = new Name();

Alternatively you could add some sort of restore function to your class that you call to reset all class variables. That way you keep the CI benefits and get to reuse it. That doesn't work if you need multiple instances simultaneously, of course.

El Forum

[eluser]mattalexx[/eluser]
Thanks again, inparo.

El Forum

[eluser]Pascal Kriete[/eluser]
My pleasure Smile .