sfDoctrinePlugin

symfonyは現在はPropelを標準のO/Rマッパーとして使用するようになっており、DoctrineというO/Rマッパーがプラグインとして提供されています。Doctrineを使えばスキーマ定義が1つでよかったり(Propelならschema.xmlに加えpropel.iniも必要)、複数のスキーマ間でリレーションを定義できたりする点でpropelにはないメリットがあります。


そして、将来的にはPropel自体も標準組み込みからプラグインでの提供に変更され、PropelかDoctrineか(もしくはその他のO/Rマッパー)を簡単に選択できるように実装される予定です。(Doctrineが標準O/Rマッパーになるとの情報も)

DoctrineはパフォーマンスでもPropelよりも優秀であるという検証結果記事もあります。リンク先では「Propel1.3(開発版)がDoctrineより速いよ!」という記事ですが、1.2系のベンチマーク結果もあります。どのようなコードで検証されたかわかりませんが、Propel1.2よりはDoctrineのほうが速いのは確かなようです。

ちなみにPropel1.3系は開発中であるのと、PDOが必要とされていることなどからsymfonyがサポートするPHPのバージョンの関係も絡みsymfonyでは正式にサポートされておらず、sfPropel13Pluginを導入することで利用することが可能です。

というわけで、アシアルさんところのブログでも紹介されているように、Doctrineも実用的なレベルになりつつあり、DB操作が絡む他のプラグインなどでもPropelとDoctrineの両方に対応しているものもあります。

現在は1.1系に向けての開発が行われているため、利用するためには、plugin-installではなくsvn coでソースを取得してくる必要があります。

以下導入までのメモ。

http://trac.symfony-project.com/wiki/sfDoctrinePlugin
http://trac.symfony-project.com/wiki/sfDoctrinePlugin1.1
http://www.phpdoctrine.org/index.php/

プロジェクトルート直下で以下のコマンドでplugins直下にcheckoutする

$ svn co http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.0 ./plugins/sfDoctrinePlugin

追記:
sfDoctrinePluginには0.1, 1.0, 1.1の3系統のブランチがありますが、symfony1.0系を使うのであればsfDoctrinePlugin1.0を使うようにします。sfDoctrinePlugin1.1はsymfony1.1から利用できる新しいタスクコマンドに対応しているという点のみ異なるようです。uheuheさんからご指摘いただきました。ありがとうございました。

doctrineのセッティングのためのファイルをプロジェクトのconfigにコピーする

$ cp -p ./plugins/sfDoctrinePlugin/config/doctrine.yml ./config

./config/databases.ymlにdoctrineの設定に変更

all:
myConnection:
class: sfDoctrineDatabase
param:
dsn: mysql://symfony:password@localhost/sample

どんなdoctrineのタスクが用意されているかを見てみると

doctrine-build-all > doctrine build all - generate model and initialize database, drops current database if exists
doctrine-build-all-load > doctrine build all load - generate model, initialize database, and load data from fixtures. Drops current database if exists
doctrine-build-db > doctrine build database - initialize database, drop current database if exists
doctrine-build-model > build Doctrine classes
doctrine-build-schema > doctrine build schema - build schema from an existing database
doctrine-build-sql > exports doctrine schemas to sql
doctrine-drop-all-tables > doctrine drop all - drop all database tables
doctrine-drop-db > doctrine drop database - drops database
doctrine-dump-data > dump data to yaml fixtures file
doctrine-generate-crud > Creates Doctrine CRUD Module
doctrine-import > converts propel schema.*ml into doctrine schema
doctrine-init-admin > initialize a new doctrine admin module
doctrine-insert-sql > insert sql for doctrine schemas in to database
doctrine-load-data > load data from yaml fixtures file
doctrine-load-nested-set > load doctrine nested set data from nested set fixtures file

propel-***と同等のタスクが用意されているのがわかりますね。
propelを使ってきた人のためにpropelのスキーマからdoctrineのスキーマへの変換コマンドまであります。
追記:上記一覧は手元の0.1系のタスク一覧ですので、1.0系とは異なるようです。また、Propelからスキーマの変換はひと手間必要なようです。uheuheさんコメントも参考にしてみてください。

個人的にはプラグインをガシガシ使う人間なので、無難にPropelを選択してしまいますが、将来的にDoctrineが正式にサポートされればDoctrineを利用するようになるような気がします。