Using ActiveRecord Library Separately from CodeIgniter

31 thoughts on “Using ActiveRecord Library Separately from CodeIgniter”

  1. Looks good,
    you should be able to use


    instead of


    No big deal but looks just a bit cleaner.

  2. You can also check out myActiveRecord, a standalone ActiveRecord class for mysql (as of right now, hopefully more db support soon). I tried to make the syntax as simple as possible to use. Its slightly different from CI, in that it uses camelcase dynamic functions, like: $db->findUsernameWhere(“id=$id”);


  3. thank you very much for this!

    one thing: in DB_driver.php, there is a method display_error that uses a library from CodeIgniter. i think it’s a good idea to change it for printing out the error message and die, instead of using the Exceptions library.

  4. Hi there. This is a great idea. I have followed your instructions but PHP is generating an error at line 18: “private static $dsn;”

    Any ideas why this might be? Also as ibnoe has mentioned, the download URL is broken. Any chance of fixing this up? 🙂

  5. its me again. i wanted to tell you again that after integrating your ci active_record, my coding live changed completely – and that makes me very happy. my code looks wonderful now, I threw out that adodb – which i really hated from all the beginning.

    1.000.000 thanks for that!

    i hope very much you go on to extract perhaps the complete loader class or maybe more ci classes as the very nice template or validation…

    please do more of that, its fantastic to work with the separated ci classes in existing projects!

  6. Thanks for this. I had just decided that CodeIgniter wasn’t quite right for me. However, I loved ActiveRecord; so, I was going to do the same thing you’ve done.

    Now that I’ve found this, I can just get started instead of having to figure it out myself.

  7. Could you post a php4 version? I tried to do it myself thinking you could just switch up a few things. But unfortunatley it didnt work.

  8. With tCI 1.7 this is no longer needed, now there is a function DB() in the which can receive a dsn as parameter. All you need to do is define BASEPATH and EXT, include “database/DB.php” and define function log_message(){}

    Works like a charm! 🙂

  9. @eljunior: Not if you want to use the active record set parts of the class.

    Theres a whole bunch of methods that need to be over-riden etc.

  10. Nice.. My variant is
    [code language=”lang="php”]
    @define(BASEPATH,’./core/’);//folder, contain database classes
    function log_message($text){
    /*echo ‘<div style="color:green"><h3>Log</h3>’.$text.'</div>’;*/
    function instantiate_class($class){
    return $class;
    $DB = DB();
    $query = $DB->get(‘users’);
    foreach ($query->result_array() as $row)
    no Changes in Classes
    Can get config like CI from APPPATH/config/database.php

  11. @Michail1982: Excellent tip, thanks !

    If someone doesn’t want to use the config file, they can just call the DB() function with the dsn like this:

    $db = DB("mysql://root:test123@localhost/storyteller");

  12. I am not able to download the zip file.. its looking dead link.

    and if goes according to the step mentioned above.. i m getting the following error.

    Fatal error: Call to a member function num_rows() on a non-object

  13. require_once(‘core/database/DB.php’);
    function instantiate_class($class){
    return $class;

    will this execute if i put it in my code or are those just name spaces . I know core refers to the base path what of instantiate_class ? Also $class should be the DB function right, cause i am looking at it right now and i dont see where that block of code comes in.

    1. Dear Sulaemanhow are you?I’m just playing aurond with the balita theme. Now I’m at a critical point. When I open the shop, at the first page are just product pictures. Is it also possible to show categorie pictures instead? That would be pretty cool.Is it possible and how much would it cost to chage it like that? Thank you very much for your help.With best regards,Tobi

  14. Hello there everyone,
    First I’d like to thank You hasin for excellent library separation. Now I don’t know if You’ve heard of Smarty templates engine, but I’m trying to use it with this library, and I’ve faced couple of problems.
    Basically my code should work like this:
    And i got a problem with assigning the db result to smarty array:

    If You can figure out a way for me to do this, I would appreciate it soo much.

Leave a Reply

Your email address will not be published. Required fields are marked *