Doctrine vs. Propel: 2009 update

最好的PHP ORM库, DoctrinePropel.

特 性
Doctrine和Propel都有类似的基本特性: 他们都支持基本的CRUD操作。

两者都能产生模型类,Propel基于XML,Doctrine基于YAML.
都支持从存在的数据库产生各自的配置文件,当然不是百分百精确——这是由于某些数据库独有特性的使用不能产生合适的配置标记。

高级特性

两者支持使用嵌套set模型存储树结构。Doctrine的嵌套set实现支持存储多个树在一同一张表。

两者也都支持模型的数据校验和模型关系。

They also support single table inheritance, although in Doctrine this is known as concrete inheritance. Doctrine supports two other inheritance models: Simple, where all classes have the same columns, and column aggregation, which stores an additional type value in the table, allowing automatic instanciation of the correct model type when querying.

And here ends the shared features. All of the following things are features that only Doctrine has.

Behaviors: Doctrine supports applying various “behaviors” to your models, for example a Timestampable model automatically gets two columns: created_at and updated_at, which get a time when a row is first created and when it’s updated respectively.

搜索:Doctrine有一个全文搜索引擎。

Data fixtures and migrations. Caching, events, pagination, command line interface… you might say that Doctrine beats Propel hands down when it comes to more advanced features.

易用性

文 档

The first thing is of course the documentation. Without decent documentation it’ll be difficult to use any library. Last year, Propel’s documentation was one of the main problems with it - and it still hasn’t gotten any better.

On the opposing side, the Doctrine team has been constantly improving their already superior documentation, and they are even working on a paperback Doctrine book. Documentation is a clear win for Doctrine.

库的使用

The first task you will have with both of the libraries is creating the model classes. Doctrine allows you to write simple YAML markup, or straight PHP code if you prefer it that way. If you use YAML, Doctrine has some methods that you can call in your own code, or you can download a command-line interface for building your models.

Propel’s approach to creating models requires you to write XML. To build your models from XML, you also need Phing. Personally I find XML more complex to write by hand than YAML, and requiring additional libraries is a bit of a hassle, unless you already use Phing for something else such as build automation.

数据库操作
Basic CRUD operations are quite similar in both. However, there’s a big difference in the way more precise queries are done.

Propel uses a criteria/peer approach:
$c = new Criteria();
$c->add(ExamplePeer::ID, 20);
//SELECT all "Example" models which have 20 as their ID and join all foreign tables.
$items = ExamplePeer::doSelectJoinFoobar($c);

2010年Propel1.5的全新API很好用
<?php
$book = BookQuery::create()->findPK(123); // retrieve a record from a database
$book->setName('Don\'t be Hax0red!'); // modify. Don't worry about escaping
$book->save(); // persist the modification to the database

$books = BookQuery::create()  // retrieve all books...
  ->filterByPublishYear(2009) // ... published in 2009
  ->orderByTitle()            // ... ordered by title
  ->joinWith('Book.Author')   // ... with their author
  ->find();
foreach($books as $book) {
  echo  $book->getAuthor()->getFullName();
}

Doctrine’s approach is to use Doctrine_Query and a custom SQL dialect, DQL:
$items = Doctrine_Query::create()
       ->from('Example e')
       ->leftJoin('e.Foobar')
       ->where('e.id = ?', 20)
       ->execute();

I think Doctrine’s approach lends itself better to the purprose. The code is also easier to read, as it flows more naturally. Propel’s version does take a bit less code.

Setting values on model classes is also a bit different: Doctrine uses magic properties, while Propel generates methods for setting and getting values. This gives Propel code the advantage of autocompletion in more IDE’s - as far as I know, only the latest versions of NetBeans can autocomplete Doctrine’s magic properties, thanks to their support of the @property PHPDoc annotation.
Conclusion

不管你怎么看待它,Doctrine是更好的,其更好的文档,更好的特性并且社区更活跃。Propel在进步,但是Doctrine进步得更快。

有好事者做了ORM Visual Editor: http://www.orm-designer.com/ 可以用在项目中。

你可能感兴趣的:(xml,PHP,orm,搜索引擎,Netbeans)