10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1: Introduction 15<br />

layer dep<strong>en</strong>ds on the DBAL and therefore, wh<strong>en</strong> you load the ORM package the DBAL is<br />

already included.<br />

<strong>Doctrine</strong> Explained<br />

The following section tries to explain where <strong>Doctrine</strong> stands in the world of ORM tools. The<br />

<strong>Doctrine</strong> ORM is mainly built around the Active Record 2 , Data Mapper 3 and Meta Data<br />

Mapping 4 patterns.<br />

Through ext<strong>en</strong>ding a specific base class named <strong>Doctrine</strong>_Record, all the child classes get<br />

the typical ActiveRecord interface (save/delete/etc.) and it allows <strong>Doctrine</strong> to easily<br />

participate in and monitor the lifecycles of your records. The real work, however, is mostly<br />

forwarded to other compon<strong>en</strong>ts, like the <strong>Doctrine</strong>_Table class. This class has the typical<br />

Data Mapper interface, createQuery(), find(id), findAll(), findBy*(),<br />

findOneBy*() etc. So the ActiveRecord base class <strong>en</strong>ables <strong>Doctrine</strong> to manage your records<br />

and provides them with the typical ActiveRecord interface whilst the mapping footwork is<br />

done elsewhere.<br />

The ActiveRecord approach comes with its typical limitations. The most obvious is the<br />

<strong>en</strong>forcem<strong>en</strong>t for a class to ext<strong>en</strong>d a specific base class in order to be persist<strong>en</strong>t (a<br />

<strong>Doctrine</strong>_Record). In g<strong>en</strong>eral, the design of your domain model is pretty much restricted<br />

by the design of your relational model. There is an exception though. Wh<strong>en</strong> dealing with<br />

inheritance structures, <strong>Doctrine</strong> provides some sophisticated mapping strategies which allow<br />

your domain model to diverge a bit from the relational model and therefore give you a bit<br />

more freedom.<br />

<strong>Doctrine</strong> is in a continuous developm<strong>en</strong>t process and we always try to add new features that<br />

provide more freedom in the modeling of the domain. However, as long as <strong>Doctrine</strong> remains<br />

mainly an ActiveRecord approach, there will always be a pretty large, (forced) similarity of<br />

these two models.<br />

The curr<strong>en</strong>t situation is depicted in the following picture.<br />

Relational Bounds<br />

As you see in the picture, the domain model can't drift far away from the bounds of the<br />

relational model.<br />

After m<strong>en</strong>tioning these drawbacks, it's time to m<strong>en</strong>tion some advantages of the ActiveRecord<br />

approach. Apart from the (arguably slightly) simpler programming model, it turns out that the<br />

strong similarity of the relational model and the Object Ori<strong>en</strong>ted (OO) domain model also has<br />

an advantage: It makes it relatively easy to provide powerful g<strong>en</strong>eration tools, that can create<br />

a basic domain model out of an existing relational schema. Further, as the domain model<br />

can't drift far from the relational model due to the reasons above, such g<strong>en</strong>eration and<br />

synchronization tools can easily be used throughout the developm<strong>en</strong>t process. Such tools are<br />

one of <strong>Doctrine</strong>'s str<strong>en</strong>gths.<br />

We think that these limitations of the ActiveRecord approach are not that much of a problem<br />

for the majority of web applications because the complexity of the business domains is oft<strong>en</strong><br />

moderate, but we also admit that the ActiveRecord approach is certainly not suited for<br />

complex business logic (which is oft<strong>en</strong> approached using Domain-Driv<strong>en</strong> Design) as it simply<br />

puts too many restrictions and has too much influ<strong>en</strong>ce on your domain model.<br />

<strong>Doctrine</strong> is a great tool to drive the persist<strong>en</strong>ce of simple or moderately complex domain<br />

models(1) and you may ev<strong>en</strong> find that it's a good choice for complex domain models if you<br />

consider the trade-off betwe<strong>en</strong> making your domain model more database-c<strong>en</strong>tric and<br />

2. http://www.martinfowler.com/eaaCatalog/activeRecord.html<br />

3. http://www.martinfowler.com/eaaCatalog/dataMapper.html<br />

4. http://www.martinfowler.com/eaaCatalog/metadataMapping.html<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!