• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connection to DB from library

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?

Why not just use the inflector helpers and in your models create a function similar to this:


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

  return $result

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:
| 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:
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)

I'm actually going to build them like this:

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.


Oh yes, I'm aware that Buton don't do goggles!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.