ActiveRecord in PHP

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.

3 Comments »

  1. Kalle Henriksson said,

    May 10, 2009 @ 4:20 pm

    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!

  2. Luke said,

    May 10, 2009 @ 4:52 pm

    Kalle,

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

    $ps->productcategories_push($pc);

  3. Kalle Henriksson said,

    May 11, 2009 @ 3:25 am

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

RSS feed for comments on this post · TrackBack URI

Leave a Comment