Welcome Guest, Not a member yet? Register   Sign In
Using the Active Record library as a standalone
#1

[eluser]Frunkie[/eluser]
Hey everyone,

What I would like to do is to take CIs active record library and use it as a standalone for one application that I am working on. I have searched and not been able to find anything on this. Can someone tell me if this has been done or maybe someone has knowledge of how to do this? All I need is the mysql lib.

Thanks!
#2

[eluser]xwero[/eluser]
AR is not a stand alone library, it is a part of the database library. The latter is the most complex library of CI.

But you can use the database library as a stand alone library with a few changes and strip off all the things you don't need.
#3

[eluser]Frunkie[/eluser]
Your right, AR is a part of the DB library. My bad. I'm really new to CI and fairly new to OOP so please bear with me.

What I have done is stripped out the database directory from the CI build and put that in the root directory of my server.

I edited the DB.php file and put my dsn info into the DB function ($params=''). I defined my BASEPATH and EXT in the DB.php file as well.

I created a test.php file in the same directory and included the DB.php file. How do I run a query from there? Am I missing something else?

Also, thank you for your fast reply!
#4

[eluser]xwero[/eluser]
The library structure is not that good because the main file, DB.php, is stored in the database directory. The directory should be to store every file except the main file then you can load the library as follows
Code:
include('libraries/DB.php');
instead of
Code:
include('libraries/database/DB.php');

Another thing you have to strip from CI is the application/config/database.php file. The inclusion of this file should be altered too.

To make method calls you do
Code:
$db = new CI_DB();
$db->query('sql statement here');
#5

[eluser]Frunkie[/eluser]
Ok, I made the changes that said to make but I am getting this error:
Quote:PHP Fatal error: Class 'CI_DB' not found

I looked for that class but it is non-existent. Unless that class was outside the database directory, it was not deleted.

The only directory that I kept from CI was the database directory. Could this be my problem?
#6

[eluser]Frunkie[/eluser]
The only reference that I was able to find to CI_DB class is in the DB.php file.

Any suggestions?

Code:
require_once(BASEPATH.'DB_driver'.EXT);

    if ( ! isset($active_record) OR $active_record == TRUE)
    {
        require_once(BASEPATH.'DB_active_rec'.EXT);
        
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }
    }
    else
    {
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }
#7

[eluser]oddman[/eluser]
Why don't you use a database abstraction layer that isn't tied to any specific library? There are plenty of them around that use the ActiveRecord pattern.
#8

[eluser]Frunkie[/eluser]
[quote author="oddman" date="1230962154"]Why don't you use a database abstraction layer that isn't tied to any specific library? There are plenty of them around that use the ActiveRecord pattern.[/quote]
If you could point me to one I would be forever grateful. The reason I chose CIs was because it is mature and stable. I have seen some AR examples around and they don't look all that great. I don't know that much about AR to be able to tell what is good and what is not.

Yeah, if you or anyone else can recommend something stable, that would be great. Even someone that may be able to tell me exactly how to extract it from CI would be cool too.

Thanks!
#9

[eluser]Popcorn[/eluser]
Try using PDO for a database abstraction layer.

You also might be interested in doctrine and propel
#10

[eluser]Frunkie[/eluser]
Thanks guys. It looks like maybe PDO will be the best way for me to go.




Theme © iAndrew 2016 - Forum software by © MyBB