tags:

views:

4066

answers:

10

I'm looking for Ruby's Active record for PHP. Something that is so simple that I just define my fields, extend the base ORM class, and I get ACID operations for free. I should get default getters and setters without writing any code, but overriding a default getter or setter is as easy as declaring get$fieldName or set$fieldName functions with the behavior I want. Symphony makes you create about 5 files per object, and all defined objects always load as far as I can tell. What is a better alternative? Why is it better? Can you put simple examples in your answers please?

Doctrine is another ORM I've looked at besides symphony . There also you need to create yaml files that describe your data structures. The database already defines this stuff. What will just read my table defs without having to generate and store config files everywhere?

+4  A: 

Zend_Db_Table and Zend_Db_Table_Row are fairly good at what you're describing. You don't need any configuration file, most metadata is "discovered" from the database itself.

Technically these classes don't implement the ActiveRecord pattern. Instead, they implement the Table Data Gateway and Row Data Gateway patterns. Together, these offer similar value as ActiveRecord, and in some ways are more flexible than ActiveRecord.

But as with any ORM, there are inevitably some SQL queries and operations that you can't do through the OO interface. No ORM can serve as one-stop shopping.

Footnote: I worked on the Zend Framework project for a little over a year, especially on the Zend_Db component. But I don't work for them anymore.

Bill Karwin
+3  A: 

Both CodeIgniter (http://codeigniter.com/user_guide/database/active_record.html) and its PHP5 only fork Kohana (http://docs.kohanaphp.com/libraries/orm) contain implementations of the ActiveRecord pattern.

jakber
Kohana seems to be the thing I am looking for. Zend's active record doesn't do any interdependencies, and in fact just gives you arrays back, not classes that you can operate on, or extend the functionality of. So Kahana looks to be the winner so far.
Zak
Zak, that's not true. Zend's Table methods return objects of type Zend_Db_Table_Row or Zend_Db_Table_Rowset. You can extend these classes. You can declare dependencies to other tables in your Zend_Db_Table classes. But whatever -- I'm sure Kahana will be adequate for your needs.
Bill Karwin
Thanks for the update. I've confirmed you're right on the Zend stuff. (Like I ever doubted ;)
Zak
+3  A: 
SchizoDuckie
A: 

You might look into CoughPHP or Propel.

Bob Somers
+7  A: 

I'm a big fan of Doctrine which is a full featured ORM that will be replacing Propel as Symfony's default ORM.

It's got your basic ORM stuff you'd expect along with a full featured query builder that I've found to be wonderful.

It comes with a full suite of command line tools to manage your databases. For example, you can create your schemas and fixtures in YAML, have Doctrine generate classes based on your Schema, create the database, create the schema based on the models, then populate the database with your fixtures all with a single ./doctrine build-all-reload.

It also includes support for database migrations and recently updated the migrations to automatically diff and generate your migration models.

As per your doctrine complaints, you can run a command ./doctrine generate-models-db or ./doctrine generate-yaml-db to automatically create models and yaml files respectively from your current database setup.

Other niceties include "Behaviors" which makes life much easier when implementing certain, well, behaviors in your schema. For example you can add the "Timestampable" behavior to your class file. Doctine automatically adds a 'created_at' and 'updated_at' column, populates them, and every $object->save() you run automatically updates the 'updated_at' column. More complex behaviors include i18n, table versioning, and trees (though really only NestedSet).

Personally I've been extremely enamored with Doctrine and rave about it every chance I get.

dcousineau
+1  A: 

Check Maintainable framework. Although I prefer code generation over ActiveRecord (runtime reflection), I found Maintainable framework easy to use especially in terms of ORM features.

http://framework.maintainable.com/mvc/3_model.php#c3.7

If you want a framework based on code generation, try QCodo. Whatever dcousineau said for Doctrine, I can say for Qcodo. This is an event driven full-fledged framework imitating .NET/Delphi. However you can just code generation feature and find ways to dissociate your generated classed from the rest of the framework. Thus, you can embed generated classed within other frameworks.

+1  A: 

Another option that follows Ruby DataMapper's implementation is phpDataMapper. It's obviously a Data Mapper instead of an ActiveRecord :).

Vance Lucas
+1  A: 

I use a little known orm layer called redbean. you can find it here : http://www.redbeanphp.com. its absolutely unique in the sense that it just creates tables columns and indexes all by itself without any configuration files at all. I find it to be a huge timesaver!

JernF
A: 

I would recommend Doctrine with Symfony. Eventhough there is more to learn you will find it has the features you will need once the project grows (CRUD, Form framework, Record Templates, DQL, Plugin support, Behaviours). Both projects have very active community and you shouldn't find yourself in a dead end, because most of your questions have been already answered in official tutorials or in a forum.

If you don't like database definitions in YAML, you can always use ORM Designer or MySQL Workbench.

Frantisek Troster
A: 

The time you spend checking all this frameworks that copy Rails you'll learn Rails itself.

daniel
What good will learning rails do me when 1) I've already used it successfully and know how it works and like it just fine, and 2) can't use rails on a huge PHP based project that has tons of legacy PHP code I need to work with.
Zak