lukebaker.org

lukebaker.org

ActiveRecord in PHP

with 3 comments

I’ve been meaning to release this code for quite some time. I’ve finally had a bit of time and motivation to sit down and take care of it. Ever since I started working with Ruby on Rails, I would cringe whenever I had to write yet more PHP code to do simple CRUD actions. Furthermore I really enjoyed the Rails syntax that you use to interact with its implementation of the ActiveRecord pattern. As such, I went forward and built a clone using PHP5. So far it has been used for a few projects and has been a joy to use.

Props to my employer, Gospel Communications, who allowed me to spend some time on this during work hours, as well as give me full copyright of the code.

More information and documentation over on the project’s home page.

Written by Luke

September 24th, 2007 at 6:25 pm

3 Responses to 'ActiveRecord in PHP'

Subscribe to comments with RSS or TrackBack to 'ActiveRecord in PHP'.

  1. Hi…!

    Thanks for sharing your code! A great piece of work. I’ve used Ruby on Rails a bit myself and really missed its ActiveRecord implementation when coding PHP. I’ve got a question about your code. I have a many2many relationship between products and categories via the table products2categories. Which is the correct way of adding a new category and appending it to a products collection of categories updating the table products2categories? It wasn’t as easy as writing…

    $ps = Product::find(‘first’, array(‘conditions’ => “`text` = ‘Prod1′”));
    $ps->productcategories_push(new Productcategory(array(‘text’ => ‘Cat2′)));

    This gave me a HasManyThroughCantAssociateNewRecords exception.

    Can you help?

    Thanks!

    Kalle Henriksson

    10 May 09 at 4:20 pm

  2. Kalle,

    You need to first create and save the Productcategory. Then you can call:

    $ps->productcategories_push($pc);

    Luke

    10 May 09 at 4:52 pm

  3. Thanks a lot! That fixed it…! ;o)

    Kalle Henriksson

    11 May 09 at 3:25 am

Leave a Reply