Is it worth using Doctrine 2 with Zend Framework?

2019-03-13 05:21发布

问题:

I know that some users use Doctrine 2 instead of Zend_Db in Zend Framework. But I don't know why. Why is Doctrine2 better than Zend_Db and why Zend_Db is not good?

Thanks

回答1:

(7-Mar-2013) Disclaimer: This answer is probably now a bit out of date. I'm not keeping up with the PHP community at the moment and this comparison is between Doctrine ORM v2 and 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).

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. 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. The generated SQL also takes advantage of transactions which gives you a decent performance gain!

I'd recommend you read up on Domain-Driven Design to get a better understanding of why Doctrine 2 is so awesome.

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.



回答2:

If you have a small project wich is bound to use a specific DBMS, you do not need ORM and 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

As you can read in the Doctrine description:

Doctrine 2 is an object-relational mapper (ORM) for PHP 5.3.0+ that provides transparent persistence for PHP objects. It sits on top of a powerful database abstraction layer (DBAL). Object-Relational Mappers primary task is the transparent translation between (PHP) objects and relational database rows.



回答3:

Zend_DB and Doctrine uses different ways. Zend_DB works like table data gateway and row data gateway. Doctrine is object mapper.

In my experiences Zend_DB enough and fast for most common tasks. Doctrine is slow and uses more memory than Zend_DB.