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
- PHP5
- Naming of tables and columns that follows the Rails convention.
Related Pages
11 Comments »
RSS feed for comments on this post · TrackBack URI
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?
Luke said,
February 12, 2008 @ 1:27 pm
Kevin, it most certainly is open source.
http://lukebaker.org/svn/repos/activerecord/trunk/LICENSE.txt
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.
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.
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.
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.
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?
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 …
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?
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!
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