CodeIgniters ActiveRecord library is a nice implementation (though modified) of ActiveRecord design pattern. It comes bundled with CodeIgniter. So if you want to use Just this library in your application individually instead of using full CodeIgniter framework, what to do?
Today I’ve spent some time and separate the ActiveRecord library from CodeIgniter. Now you can use it via a class named ActiveRecordFactory. Here’s how to do it (I separate only the MySQL driver) .
I separate the “database” folder under the “system” directory of CodeIgniter. Then I removed all folders in database/drivers except “mysql”. (I need only MySQL,
)
I modified the class definition at database/drivers/mysql_driver.php
Previously It Was : class CI_DB_mysql_driver extends CI_DB {
Now : class CI_DB_mysql_driver {
I modified the class definition at database/DB_driver.php
Previously It Was : class CI_DB_driver {
Now : class CI_DB_driver extends CI_DB_mysql_driver {
And finally I create a ActiveRecord Factory to instantiate codeigniters Active Record Properly. Here is the code
File: database/ActiveRecordFactory.php
<?
/**
* This is a Factory to create an instance of Code Igniters Active Record Class
*
* @author Hasin [http://hasin.wordpress.com]
*/
define(BASEPATH,"./");
include_once ("drivers/mysql/mysql_driver.php");
include_once ("DB_driver.php");
include_once ("DB_active_rec.php");
include_once ("DB_result.php");
include_once ("drivers/mysql/mysql_result.php");
function log_message(){/*just suppress logging*/}
Class ActiveRecordFactory {
private static $dsn;
private static $CIAR;
public static function Factory($dsn=null)
{
if (!empty($dsn)) self::$dsn = $dsn;
if (empty(self::$CIAR))
{
if (!empty(self::$dsn))
self::$CIAR = new CI_DB_active_record($dsn) ;
else
throw new Exception("Please give a DSN in this format 'driver://username:password@hostname/database'");
}
return self::$CIAR;
}
}
?>
Now You can test The ActiveRecord library like this
<?
include(“database/ActiveRecordFactory.php”);
$activerecord = ActiveRecordFactory::Factory(“mysql://user:password@localhost/test”);
$activerecord = ActiveRecordFactory::Factory();
$activerecord->select(“name”);
$activerecord->where(“id=”,”10″);
$activerecord->orwhere(“id=”,”19″);
print_r($activerecord->get(“users”)->result_array());
$activerecord->select(“name”);
$activerecord->where(“id=”,”10″);
$activerecord->orwhere(“id=”,”19″);
echo $activerecord->get(“users”)->num_rows();
$activerecord->insert(“users”,array(“name”=>”CodeIgniter”,”pass”=>”CI Rocks”));
?>
You can download the complete example from here http://hasin.javapark.net/ActiveRecord.zip
Pingback: developercast.com » Hasin Hayder’s Blog: Using ActiveRecord Library Separately from CodeIgniter
Salam via
thanks for modifying the CI DB’s ActiveRecord , it will help us to do small projects more quicker.
Looks good,
you should be able to use
$activerecord->where(”id”,”10″);
$activerecord->orwhere(”id”,”19″);
instead of
$activerecord->where(”id=”,”10″);
$activerecord->orwhere(”id=”,”19″);
No big deal but looks just a bit cleaner.
Pingback: PHPGeek » Standalone ActiveRecord Library from CodeIgniter
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”);
Thanks!
Wess
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.
Server not found
can’t find the server at hasin.javapark.net.
can u send to my email? please
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?
thanks for this, saved my day
still waiting for a superclean rails like orm in php…
greetings
I found the correct URL for the example files:
http://javapark.net/hasin/ActiveRecord.zip
works great, thanks!
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!
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.
Pingback: Super Scratch Development-1: Ideas from CodeIgniter & Drupal « Everyone should get a 2nd chance
Pingback: Using Validation Library Separately from CodeIgniter « Everyone should get a 2nd chance
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.
Hi!. I spent a little of my time and updated the library!! Only needs to say which driver to use. At this time, only mysql and postgre works… So… here is the link. It’s easy to use. Just say which driver to use… and that’s it.
http://cid-c12a38c5716b43a6.skydrive.live.com/self.aspx/Documentos/ActiveRecord.zip
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!
@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.
hey, I’ve a download error.. is the server down?
Pingback: Documentopia » Code CodeIgniter ActiveRecord – Stand Alone Version
good…
Nice.. My variant is
@define(BASEPATH,’./core/’);//folder, contain database classes
@define(APPPATH,’./’);
@define(EXT,’.php’);
function log_message($text){
/*echo ‘<div style="color:green"><h3>Log</h3>’.$text.’</div>’;*/
}
require_once(‘core/database/DB.php’);
function instantiate_class($class){
return $class;
}
$DB = DB();
$query = $DB->get(‘users’);
foreach ($query->result_array() as $row)
{
print_r($row);
}
no Changes in Classes
Can get config like CI from APPPATH/config/database.php
@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");
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
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.
please, In the above message, my delete web addres…
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
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:
http://pastebin.com/yaBz4nHe
And i got a problem with assigning the db result to smarty array:
http://pastebin.com/V1FBhYNG
If You can figure out a way for me to do this, I would appreciate it soo much.
Regards,
Sim00n.
thank u for this ,very good work;
well i will try it ;thank You for spend ur time.
have a nice day