Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6963715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:49:48+00:00 2026-05-27T15:49:48+00:00

I’m fairly new to CodeIgniter, and am trying to figure out something I feel

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:49:49+00:00Added an answer on May 27, 2026 at 3:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.