0

I'm fairly new to CodeIgniter, and am trying to figure out something I feel should be easy that isn't.

My application does a bit with Product objects, which at the momenet I'm just doing via the ActiveRecord features included within CodeIgniter's Database class. I normally prefer ORM over ActiveRecord, so as to properly separate business logic from database logic. In this case though, the objects that use Products generally handle all the business logic themselves, so I think the separation is still valid.

My Product objects interact with the database through a store() method which I'd like to have accept a database connection as a parameter. The store() method first queries the database to determine if the product in question already exists in some form. If so, the product is updated with new information, and if not the product is inserted. Easy stuff. The problem is, I want to use type hinting for the store() method parameter, so that I can be sure that no objects incapable of basic CodeIgniter database/ActiveRecord functions are passed in to the method without the appropriate error being raised.

For the life of me, I can't figure out the type of the CodeIgniter database class. After reading through the source code a bit, I thought for a while that the CI_DB_driver class was the one. However, when I use this as a type hint, my IDE (NetBeans 7.01, to be exact) doesn't recognize or give me any code completion options for ActiveRecord functions like insert() or get_where(). I don't think my IDE is at fault in this, in that it does recognize methods like query() or insertString().

I could use the standard CodeIgniter way of accessing/connecting to databases, i.e. using the internal CodeIgniter object's load method to instantiate a database object, but I was kind of hoping to insert or update Products as part of a somewhat bastardized Unit of Work pattern. I don't need to use transactions, since Product objects do not depend on the successful insertion, or update of any other Product objects (nor do the classes using Product objects). However, I don't want to create a brand new connection for every single Product object to be inserted or updated, nor do I want to tie things up with a persistent database connection. I'd rather just do something more akin to the following:

// catalog class is just an example
class Catalog extends CI_Model
{
    public function updateCatalog(array &$products, $dbconn = null) {
        if ($dbconn === null) {
            try {
                $dbconn = $this->load->database('default', true);
            } catch (SQLException $e) {
                show_error($e->getMessage);
            }
            $close_db = true;
        }
        foreach ($products as $product) {
            try {              
                // use the same db connection for all product storage tasks
                $product->store($dbconn);
            } catch (SQLException $e) {
                show_error($e->getMessage);
            }
        }
        if ($close_db) {
            $dbconn->close(); // free up database connection for other tasks
        }
    }
...

This is a very basic example. If another method needed to call the above method, it would instantiate the database connection object itself, and pass it in. I'm aiming for one database connection per overall request, ideally.

Has anyone delved deeply enough into the CodeIgniter source code to know what I should use as a type hint for database objects? Is what I'm doing a bad approach to use with CodeIgniter? Would it be better to override the CodeIgniter functionality with a means of implementing connection pooling?

hakre
  • 193,403
  • 52
  • 435
  • 836
xobicvap
  • 292
  • 2
  • 11

2 Answers2

1

The type of the database object returned by $this->load->database() is in fact CI_DB_driver, which is what I was hoping it would be. In actuality the type is CI_DB_(insert your database type)_driver, but this inherits from CI_DB_driver.

I don't know if this will ever be helpful to anyone, but I'm putting this out there just in case.

xobicvap
  • 292
  • 2
  • 11
0

Not sure what you mean by type hint, but it looks like you do not want to use the CodeIgniter Database Class. So why don't you include external ORM class you like instead of messing around with CodeIgniter?

Check out the following resources:

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • Type hinting is where instead of my store method definition being store($dbconn), it would be (for example) store(CI_DB $dbconn). This allows me to ensure that only objects extending a certain class or implementing a certain interface can be passed into the method. ORM is kind of overkill for this problem in that the Product object I mentioned is pretty much the only one that will need any kind of custom functionality. I'll consider ORM if the application scales to where I need it. – xobicvap Dec 16 '11 at 08:14
  • Mh, I did never thought about ORM as overkill. You could find out the correct class for type hinting using `get_class`() or `get_parent_class`(). – PiTheNumber Dec 16 '11 at 08:28