CodeIgniter Forums
Connection to DB from library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Connection to DB from library (/thread-6554.html)



Connection to DB from library - El Forum - 03-03-2008

[eluser]tedward[/eluser]
I'm want to stick some functionality into a library. This library needs to connect to the db to populate some arrays. Is it 'correct' to connect to the database directly from the library or should I be creating a model for data access?





more info if you're interested...

I'm using an adjacency list to create a nested category structure. My URLs work fine using the numeric category ID's, but I want to be able to map these ID's to actual category names. This means I'll be able to construct my URLs using the text descriptions of each category. The library will return a map of ID to name and a map of name to ID.

A question a little further into development is going to be - how do I persist these maps so that they don't have to be repopulated with every category browse?


Connection to DB from library - El Forum - 03-03-2008

[eluser]mikeyhell[/eluser]
Why not just use the inflector helpers and in your models create a function similar to this:

controller
Code:
$this->model_name->getCat(humanize($this->uri->segment(3)));

Model
Code:
function getCat($catName){
  $this->db->select('id');
  $this->db->where('name', $catName);
  $query= $this->db->get('categories')
  
  if( $query->num_rows() > 0 ):
    $result= $query->result();
  else:
    $result= false;
  endif;

  return $result
}



Connection to DB from library - El Forum - 03-10-2008

[eluser]tedward[/eluser]
I appreciate the response, but it doesn't really get me any further.

I'm using an adjancency list to display my categories, their sub-categories, the sub-categories of the sub-categories etc.

The data could look like this:
Code:
+-----+----------------------+-----+
| CID | name                 | PID |
+-----+----------------------+-----+
|   1 | Snowboards           |   0 |
|   9 | Burton               |   1 |
|   5 | Eyewear              |   0 |
|  10 | Rome                 |   1 |
|  30 | Goggles              |   5 |
|  31 | Sunglasses           |   5 |
|  32 | Oakley               |  30 |
|  33 | Anon                 |  30 |
+-----+----------------------+-----+

This allows me to build URLs using the category ID's for all categories that look like this:
Code:
http://localhost/catalog          (top-level)
http://localhost/catalog/1        (Snowboards)
http://localhost/catalog/1/9      (Snowboards > Burton)
http://localhost/catalog/1/10     (Snowboards > Rome)
http://localhost/catalog/5        (Eyewear)
http://localhost/catalog/5/30     (Eyewear > Goggles)
http://localhost/catalog/5/30/32  (Eyewear > Goggles > Burton)
http://localhost/catalog/5/30/33  (Eyewear > Goggles > Anon)
http://localhost/catalog/5/31     (Eyewear > Sunglasses)
Easy.

I'm actually going to build them like this:
Code:
http://localhost/catalog          
http://localhost/catalog/Snowboards
http://localhost/catalog/Snowboards/Burton
http://localhost/catalog/Snowboards/Rome
http://localhost/catalog/Eyewear
http://localhost/catalog/Eyewear/Goggles
http://localhost/catalog/Eyewear/Goggles/Burton
http://localhost/catalog/Eyewear/Goggles/Anon
http://localhost/catalog/Eyewear/Sunglasses

When any of these URLs are requested, I need to map all the category parameters back to their category ID's. This is complicated by the fact that two categories in different locations can have the same name. I'll need to check if the category is unique - if so, return the catID - if not, move up the category structure until I find uniqueness, then return the catID.

I need to have a library class that populates itself with all the catalog data and provides methods to map category name to category ID in the easy (unique) and complicated (non-unique different location) situations. I don't need help with the logic of the methods, but I do need to know if it's ok to connect to the database directly from the library??

Also, this design seems immediately flawed, as every URL of the form shown above will cause the library to read from the DB and repopulate itself. I'd like to find a way to do this once. Java has an 'application scope' where data like this can be stored. Does anyone have any suggestions to persist this data?

I've got virtual hosting that means memory and other resources are shared across many VM's, on the principle that you get resources as you need them, so it doesn't seem polite to secure myself a block of memory with memcached? what do you think.

Sorry to have been so verbose, but a solution to this will no doubt be useful for the community.

Cheers!


Connection to DB from library - El Forum - 03-10-2008

[eluser]tedward[/eluser]
Oh yes, I'm aware that Buton don't do goggles!