What is the best practice of MVC, Doctrine2, Datamapper?

I am learning to use Doctrine2 with my Zend Framework installation. I really like the datamapper template, mainly because it shares my domain models with my database.

My question is what is the best way to use Doctrine and DQL with my controllers?

  • Controllers
  • use Doctrine DQL / EntityManager directly to save / load my domain models?

  • create my own classes in a data template to save / load my domain models and then use the Doctrine inside my own classes?

Pros. for # 1, of course, I don’t need to create my own data models, but again, with No. 2, I can later replace the Doctrine (theoretically)

What would you do?

+4
source share
5 answers

As for your question about abstraction, I would say that it really depends on the lifetime of this project and how portable your code will be. If this is a one-time website that requires minimal maintenance, it will probably save you some time to drop an extra layer of abstraction and just write the Doctrine code in your controllers. However, if you plan to reuse this code, move it to different platforms or support it for a long period of time, I would take the time to add this abstraction, because it will give you much more flexibility.

If you are still exploring the framework, check out Kohana . This is basically an easy CodeIgniter rework written for PHP5.

+3
source

I am the second tip from pix0r. An additional abstraction is worth it only if it is a large project with a potentially long life and, possibly, with many developers. This is pretty much the basic rule that I follow, also in php with doctrine2 or in java with jpa (since doctrine2 strongly resembles jpa).

If you need additional abstraction, doctrine2 already has the ability to use repositories (repositories are very similar or even equal to DAOs, perhaps with a closer look at business terms and logic). There is a base class Doctrine \ ORM \ EntityRepository. Whenever you call EntityManager # getRepository ($ entityName), Doctrine will see if you have configured a custom repository class for this object. If not, it creates an instance of Doctrine \ ORM \ EntityRepository. You can configure your own repository class for the object in metadata, for example, in docblock annotations: @Entity (..., repositoryclass = "My \ Project \ Domain \ UserRepository"). Such a custom class must inherit from the EntityRepository and invoke the parent constructor accordingly. The base class already contains some basic search functions *.

Roman

+2
source

Also consider Symfony in your research. This will allow you to use ORM (propel / doctrine), or you can write your own data model. You can also bypass data relationships and use direct SQL if necessary.

To solve your problem by 1 or 2, I personally switch from ORM to my projects and go around if necessary. And even better with symfony, you can switch between doctrine and movement or your own classes if you ever need to.

Pros: 1) faster development. easier to maintain. generally more secure and consistent. easy to switch databases should you ever need to.(from MySQL to Oracle, etc). 2) Faster runtime. Less dependency. Cons: 1) Slower runtime and larger memory footprint. Dependency on other projects. 2) (reverse the pros for #1) 
+1
source

My thoughts are like a pix0r reaction. However, if your project is small enough for you to use EntityManager / DQL directly in the controllers, then Doctrine 2 may be redundant for your project, and perhaps you should consider a smaller / simpler system.

+1
source

In my experience of writing perseverance, the logic of the level in the controller has always returned to pursue me in the form of technical duty. This seemingly small solution is now likely to trigger inevitable and potentially large-scale re-factoring in the future, even on small projects. Ideally, we would like to be able to copy and paste extremely thin controllers reconfigured with appropriate models, so as not to force us to repeatedly create CRUD controllers over and over, but also to configure these controllers. In my opinion, this can only be achieved through discipline and the maximum possible preservation of the layer of stability from our application. Since the overhead of this, especially on the cleanroom controller, is minimal, I see no reason to advise you not to do this.

0
source

All Articles