ActiveRecord in PHP

Motiviation

I wrote this after having been spoiled by Ruby on Rails’ implementation of the ActiveRecord pattern, while still needing to work primarily in PHP. When I started this, there did exist some ORM options in PHP. However, I wasn’t satisfied with any one in particular. My goals were to create an implementation that was very similar to the Rails syntax, easy to install, and fast.

Requirements

Related Pages

19 Comments »

  1. Kevin Skoglund said,

    February 12, 2008 @ 1:21 pm

    Really nice job. I was looking for a solution that would let me use ActiveRecord but without everything else that comes with a full framework.

    But you don’t say: is this open source?

  2. Luke said,

    February 12, 2008 @ 1:27 pm

    Kevin, it most certainly is open source.

    http://lukebaker.org/svn/repos/activerecord/trunk/LICENSE.txt

  3. Kevin Skoglund said,

    February 12, 2008 @ 1:36 pm

    Thanks!

    You should note that php-activerecord-0.2_r242.tar.gz does not include License.txt.

  4. Dan said,

    May 2, 2008 @ 3:05 pm

    Let me ask a dumb question. The Install page says to include the line:

    require_once ‘models/Post.php’;

    in whatever script you want to use. I don’t find a Posts.php in the models directory, though.

  5. Luke said,

    May 2, 2008 @ 3:20 pm

    Dan, the generate.php script should create that file for you. If it isn’t something doing that, perhaps it can’t connect to the database or there’s some bug in there.

  6. Dan said,

    May 2, 2008 @ 9:57 pm

    I’m sure generate.php is accessing the database as all of the model files are being generated. There’s just no Post.php.

    btw, I downloaded and installed from php-activerecord-0.2_r242.tar.gz.

  7. Tim said,

    May 16, 2008 @ 3:32 am

    Awesome! just what i was looking for. thanks.
    Do you think think project is ready for production environments?

  8. Bill Chiz said,

    May 25, 2008 @ 9:09 pm

    Thanks .. this looks great. I have setup everything as per the instructions but when I go to run generate.php I get the following wierd error … “Parse error: parse error, expecting `T_OLD_FUNCTION’ or `T_FUNCTION’ or `T_VAR’ or `’}” in /Applications/xampp/xamppfiles/htdocs/TestApp/models/activerecord/ActiveRecord.php on line 11″

    I am running Mac OS X 10.4 and using the xampp to run mysql, apache, and php 5.2.5. Read some places were they suggested it was due to using php 4 but then I rechecked my php version using the php.info page and it shows that I am running php 5.2.5. Anyone ran into similar issues? Thanks for all your help …

  9. Mark said,

    June 10, 2008 @ 8:14 am

    Great work with this implementation! One question though, is it possibly to have a 1:many relationship with a column name different to tablename_id?

    E.g. I have a table “users” containing students and tutors.
    In some other table I may have two foreign keys such as “student_user_id”, “tutor_user_id”

    Is this possible with ActiveRecord?

  10. Luke said,

    June 10, 2008 @ 8:24 am

    @Tim, I believe it is ready for production, though I’ve only put it in production on a rarely used application. I’m sure there are bugs, but I believe it to be fairly solid in general.

    @Bill, that looks like PHP4 trying to run PHP5 code. Sorry I can’t be of more help.

    @Mark, thanks. I have not done anything with more flexibility overriding the default column names. I half built it with that possibility down the road, but I couldn’t tell you how hard it would be to get that working. It certainly isn’t built in at this point, though contributions are welcome!

  11. Pet said,

    July 28, 2008 @ 12:23 pm

    Hi Luke,

    first, I would like to say I really like the class you’ve made! Sure makes programming PHP a lot easier and a lot more fun :)

    At http://www.painfullscratch.nl/code/activerecord.diff you’ll find a small patch (against revision 280) which handles these changes:

    - require_once will now require the absolute filename instead of a relative one. I’m having a project in which another “config.php” was present in the require_path. This resulted in an error.

    - classes with which there is an association will automaticly be required.

    - the is_modified() method will now only return true when the record is actually modified (I don’t know what Ruby’s behavior is?).

    Please let me know what you think.(I’ll leave for vacation tomorrowmorning, so if you would like to start a discussion, you’ll have to be patient for my reaction :)).

    Best regards,

    Patrick ‘Pet / Petski’ Kuijvenhoven
    The Netherlands

  12. Andy Jeffries said,

    January 22, 2009 @ 3:56 am

    @Bill:

    “Read some places were they suggested it was due to using php 4 but then I rechecked my php version using the php.info page and it shows that I am running php 5.2.5″

    Did you check the phpinfo from the command line (I’m assuming the generate.php is a command line script) or via Apache?

    It’s possible Apache is using your 5.2.5 whereas the command line PHP binary being found is an old 4.x one that’s still in your path.

    From the command line run “php -v” to get the version (or “php -i” to see the phpinfo generated from the command line binary).

  13. Comparatif ORM pour php. | traffic-internet.net said,

    February 6, 2009 @ 12:23 pm

    [...] Vous trouverez toutes les informations sur le site officiel. Pour les plus curieux, une définition d’active record est disponible ici et un exemple d’utilisation ici [...]

  14. Steven said,

    February 19, 2009 @ 9:44 am

    Hi Luke!

    Great!

    Post your project in Github.com repository, to sharing with comunites.

    Thnks!
    Steven

  15. Victor Zagorski said,

    April 15, 2009 @ 11:13 pm

    Hello, thanks for your lib. I’ve found it usefull.

    I’ve found a problem in it. When I use code

    $post->comments_push($comment);
    it’s ok

    but when I say
    $post->green_crocodiles_push($cro);
    It produces an error.

    It can be easily fixed, if you change following code:
    list($accoc, $func) = explode(“_”; $name; 2);
    with:
    $tmp = explode(“_”, $name);
    $func = array_pop($tmp);
    $assoc = implode(“_”, $tmp);

  16. Luke said,

    April 22, 2009 @ 7:26 pm

    Victor, thanks for the bug report. I’ve modified the file to properly find the correct association when calling those types of methods. You can either pull the updated info from Subversion or try out release 0.3 that I made today.

  17. Kalle Henriksson said,

    July 14, 2009 @ 8:06 am

    Hi..!

    Iv’e run into a small problem with a very basic test project of mine. I’ve got 3 tables: products, productcategories and productcategorization. The last one bacause och my many:many relationship between products and productcategories. The tables all have some data.

    The relationships between my models are set up as follow…

    Product.php
    protected $has_many = array(‘productcategorizations’, array(‘categories’ => array(‘through’ => ‘categorizations’)));

    Productcategory.php
    protected $has_many = array(‘categorizations’, array(‘products’ => array(‘through’ => ‘categorizations’)));

    Productcategorization.php
    protected $belongs_to = array(‘product’, ‘category’);

    So far so good… But then I run my script…

    $p){
    echo(‘Product: ‘.$p->text.”);
    foreach($p->categories as $pci => $pc){
    echo(‘Category: ‘.$pc->text.”);
    }
    echo(”);
    }
    ?>

    …and it crashes…

    I get the following long message…

    PHP Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘Category::find’ was given in blablabla…\server\models\activerecord\HasMany.php on line 64 PHP Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘Category::find’ was given in blablabla…\server\models\activerecord\HasMany.php on line 64 PHP Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘Category::find’ was given in blablabla…\server\models\activerecord\HasMany.php on line 64

    Does anyone know what I’m doing wrong? I’ll keep digging and post the solution if I come up with one. ;o)

    Thanks!

    PS. Maan I love this code. Coding PHP is sooo much easier now..!

  18. Kalle Henriksson said,

    July 14, 2009 @ 3:28 pm

    Looks like my example code went through some security checks there. ;o) Anyway… I have two simple foreach loops. One outer, going through my products and printing their names and an inner loop going through and printing the names of the categories for each product. Simple enough. But h-nooo… It fails, giving me the above error.

    Anyone?

    Thanks!

  19. Kalle Henriksson said,

    July 15, 2009 @ 2:30 am

    Sorry guys! My bad! If I had paid a little more attention to what I was doing this would not have happened. My relationship declarations in the models are wrong. Where it says “categories” it should be “productcategories”. And “categorizations” should be “productcategorizations”.

    Maan… I’ll try to hide my shame behind the fact that I’m a PHP noob :o) (but even noobs can often read and spell correctly, can’t they? Darn!) ;o)

    Thanks!

RSS feed for comments on this post · TrackBack URI

Leave a Comment