简体   繁体   English

是否值得在Zend Framework中使用Doctrine 2?

[英]Is it worth using Doctrine 2 with Zend Framework?

I know that some users use Doctrine 2 instead of Zend_Db in Zend Framework. 我知道有些用户在Zend Framework中使用Doctrine 2而不是Zend_Db。 But I don't know why. 但我不知道为什么。 Why is Doctrine2 better than Zend_Db and why Zend_Db is not good? 为什么Doctrine2比Zend_Db好,为什么Zend_Db不好?

Thanks 谢谢

(7-Mar-2013) Disclaimer: This answer is probably now a bit out of date. (2013年3月7日)免责声明:这个答案可能现在有点过时了。 I'm not keeping up with the PHP community at the moment and this comparison is between Doctrine ORM v2 and Zend Framework v1. 我目前还没跟上PHP社区,这种比较是在Doctrine ORM v2和Zend Framework v1之间进行的。 This is an apples vs. oranges comparison because they're two different things. 这是苹果与橘子的比较,因为它们是两个不同的东西。


Out-of-the-box Zend_Db is more just an enhanced Database Abstraction Layer on top of PDO, where as Doctrine 2 is a Object Relational Mapper (which sits on top of it's own DBAL). 开箱即用的Zend_Db更像是PDO之上的增强型数据库抽象层,其中Doctrine 2是一个对象关系映射器(位于它自己的DBAL之上)。

Doctrine 2 is much better for more complicated domain layers, because all your business logic, persistence logic, etc are separated over multiple classes, so they don't serve multiple roles. 对于更复杂的域层,Doctrine 2更好,因为所有业务逻辑,持久性逻辑等都是在多个类之间分离的,因此它们不能为多个角色提供服务。 Also, because you have more classes - that are cleaner and loosely-coupled - it makes testing them much easier. 此外,因为你有更多的类 - 更干净和松散耦合 - 它使测试更容易。

Futhermore, you'll be writing only fraction of the SQL that you be using Zend_Db, because you can manipulate your entity objects and Doctrine translates those change to the database. 此外,您将只编写使用Zend_Db的SQL的一小部分,因为您可以操纵实体对象,而Doctrine会将这些更改转换为数据库。 The generated SQL also takes advantage of transactions which gives you a decent performance gain! 生成的SQL还利用了可以获得不错性能的事务!

I'd recommend you read up on Domain-Driven Design to get a better understanding of why Doctrine 2 is so awesome. 我建议你阅读领域驱动设计,以便更好地理解为什么Doctrine 2如此出色。

Don't get me wrong though, you can do DDD with Zend_Db but it's not really there OOTB (because it's not an ORM), and wouldn't be nearly as powerful and full-featured like Doctrine 2. 不要误解我的意思,你可以用Zend_Db做DDD,但它并不是真的有OOTB(因为它不是ORM),并且不会像Doctrine 2那样功能强大且功能齐全。

If you have a small project wich is bound to use a specific DBMS, you do not need ORM and Doctrine. 如果您有一个必须使用特定DBMS的小项目,则不需要ORM和Doctrine。

If you have a project wich is large and in the future might need adapters to switch from one dbms to another, than you might consider using Doctrine 如果你有一个很大的项目,并且将来可能需要适配器从一个dbms切换到另一个dbms,那么你可能会考虑使用Doctrine

As you can read in the Doctrine description: 正如您可以在Doctrine描述中阅读:

Doctrine 2 is an object-relational mapper (ORM) for PHP 5.3.0+ that provides transparent persistence for PHP objects. Doctrine 2是PHP 5.3.0+的对象关系映射器(ORM),它为PHP对象提供透明的持久性。 It sits on top of a powerful database abstraction layer (DBAL). 它位于功能强大的数据库抽象层(DBAL)之上。 Object-Relational Mappers primary task is the transparent translation between (PHP) objects and relational database rows. 对象关系映射器主要任务是(PHP)对象和关系数据库行之间的透明转换。

Zend_DB and Doctrine uses different ways. Zend_DB和Doctrine使用不同的方式。 Zend_DB works like table data gateway and row data gateway. Zend_DB的工作方式类似于表数据网关和行数据网关。 Doctrine is object mapper. Doctrine是对象映射器。

In my experiences Zend_DB enough and fast for most common tasks. 根据我的经验,Zend_DB对于大多数常见任务而言足够快速。 Doctrine is slow and uses more memory than Zend_DB. Doctrine很慢并且使用的内存比Zend_DB多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM