@@ -12,11 +12,11 @@ The Model description in this section is the one used when talking about
12
12
13
13
.. note ::
14
14
15
- Model-View-Controller (MVC) is an application design pattern, that
16
- was originally introduced by Trygve Reenskaug for the Smalltalk
15
+ Model-View-Controller (MVC) is an application design pattern
16
+ originally introduced by Trygve Reenskaug for the Smalltalk
17
17
platform. The main idea of MVC is separating presentation from the
18
18
data and separating the controller from presentation. This kind of
19
- separation let's each part of the application focus on exactly one
19
+ separation lets each part of the application focus on exactly one
20
20
goal. The controller focuses on changing the data of the Model, the Model
21
21
exposes its data for the View, and the View focuses on creating
22
22
representations of the Model (e.g. an HTML page displaying "Blog Posts").
@@ -88,32 +88,33 @@ It is obvious that the above class is very simple and testable, yet it's
88
88
mostly complete and will fulfill all the needs of a simple blogging
89
89
engine.
90
90
91
- That's it! You now you know what a Model in Symfony2 is: it is any class
91
+ That's it! You now know what a Model in Symfony2 is: it is any class
92
92
that you want to save into some sort of data storage mechanism and
93
93
retrieve later. The rest of the chapter is dedicated to explaining how
94
94
to interact with the database.
95
95
96
96
Databases and Symfony2
97
97
----------------------
98
98
99
- It is worth noting that Symfony2 doesn't come with an ORM or database
100
- abstraction library of its own, this is just not the problem Symfony2 is
101
- meant to solve. However, it provides deep integration with libraries
102
- like Doctrine _ and Propel _, letting you use whichever one you like best.
99
+ It is worth noting that Symfony2 doesn't come with an object relational
100
+ mapper (ORM) or database abstraction layer (DBAL) of its own; this is
101
+ just not the problem Symfony2 is meant to solve. However, Symfony2 provides
102
+ deep integration with libraries like Doctrine _ and Propel _, which *do *
103
+ provide ORM and DBAL packages, letting you use whichever one you like best.
103
104
104
105
.. note ::
105
106
106
107
The acronym "ORM" stands for "Object Relational Mapping" and
107
108
represents a programming technique of converting data between
108
109
incompatible type systems. Say we have a ``Post ``, which is stored as
109
110
a set of columns in a database, but represented by an instance of
110
- class ``Post `` in your application. The process of transforming the
111
- from the database table into an object is called *object relation mapping *.
112
- We will also see that this term is slightly outdated as it is used in
111
+ class ``Post `` in your application. The process of transforming
112
+ the database table into an object is called *object relation mapping *.
113
+ We will also see that this term is slightly outdated, as it is used in
113
114
dealing with relational database management systems. Nowadays there are
114
115
tons of non-relational data storage mechanism available. One such mechanism
115
- is the *document oriented database * (e.g. MongoDB), for which we invented a
116
- new term "ODM" or " Object Document Mapping".
116
+ is the *document oriented database * (e.g. MongoDB), which uses a
117
+ new term, " Object Document Mapping" or "ODM ".
117
118
118
119
Going forward, you'll learn about the `Doctrine2 ORM `_ and Doctrine2
119
120
`MongoDB ODM `_ (which serves as an ODM for MongoDB _ - a popular document
@@ -123,26 +124,29 @@ this writing.
123
124
A Model is not a Table
124
125
----------------------
125
126
126
- The perception of a model class as a database table, and each individual
127
- instance as a row was popularized by the Ruby on Rails framework. It's
128
- a good way of thinking about the model at first and it will get you far
129
- enough, if you're exposing a simple `CRUD `_ (create, retrieve, update,
130
- delete) interface in your application for modifying the data of a model.
127
+ The perception of a model class as a database table, where each object
128
+ instance represents a single row, was popularized by the Ruby on Rails
129
+ framework and the `Active Record `_ design pattern. This is a good way of first
130
+ thinking about the model layer of your application, especially if you're
131
+ exposing a simple `CRUD `_ (Create, Retrieve, Update, Delete) interface
132
+ for modifying the data of a model.
131
133
132
- This approach can actually cause problems once you're past the CRUD part
133
- of your application and are trying to add more business logic. Here are
134
+ But this approach can actually cause problems once you're past the CRUD part
135
+ of your application and start adding more business logic. Here are
134
136
the common limitations of the above-described approach:
135
137
136
- * Designing schema before software that will utilize it is like digging
137
- a hole before you've identified what you need to bury. The item might
138
- fit, but most probably it won't.
138
+ * Designing a schema before the actual software that will utilize it is
139
+ like digging a hole before knowing what you need to bury.
140
+ The item might fit in the hole you dig, but what if you're burying a
141
+ large firetruck? This requires an entirely different approach if you want
142
+ to do the job efficiently.
139
143
140
- * Database should be tailored to fit your application's needs, not the
144
+ * A database should be tailored to fit your application's needs, not the
141
145
other way around.
142
146
143
- * Some data storage engines don't have a notion of tables, rows or even
144
- schema, which makes it hard to use them if your perception of a model
145
- is that it represents a table.
147
+ * Some data storage engines (like document databases) don't have a notion
148
+ of tables, rows or even a schema, making it hard to use them if your
149
+ perception of a model is that which represents a table.
146
150
147
151
* Keeping database schema in your head while designing your application
148
152
domain is problematic, and following the rule of the lowest common
@@ -151,20 +155,20 @@ the common limitations of the above-described approach:
151
155
The `Doctrine2 ORM `_ is designed to remove the need to keep database
152
156
structure in mind and let you concentrate on writing the cleanest
153
157
possible models that will satisfy your business needs. It lets you design
154
- your classes and their interactions, allowing you to postpone persistence
155
- decisions until you're ready .
158
+ your classes and their interactions first, before requiring you to even
159
+ think about * how * to persist your data .
156
160
157
161
Paradigm Shift
158
162
--------------
159
163
160
164
With the introduction of Doctrine2, some of the core paradigms have
161
- shifted. `Domain Driven Design `_ teaches us that objects are best
165
+ shifted. `Domain Driven Design `_ (DDD) teaches us that objects are best
162
166
modeled when modeled after their real-world prototypes. For example a ``Car ``
163
167
object is best modeled to contain ``Engine ``, four instances of
164
168
``Tire ``, etc. and should be produced by ``CarFactory `` - something that
165
169
knows how to assemble all the parts together. Domain driven design deserves
166
170
a book in its own, as the concept is rather broad. However, for the purposes
167
- of this guide it should be clear, that a car cannot start by itself, there
171
+ of this chapter, it should be clear that a car cannot start by itself, there
168
172
must be an external impulse to start it. In a similar manner, a model cannot
169
173
save itself without an external impulse, therefore the following piece of
170
174
code violates DDD and will be troublesome to redesign in a clean, testable way.
@@ -175,8 +179,8 @@ code violates DDD and will be troublesome to redesign in a clean, testable way.
175
179
176
180
Hence, Doctrine2 is not your typical `Active Record `_ implementation anymore.
177
181
Instead Doctrine2 uses a different set of patterns, most importantly the
178
- `Data Mapper `_ and `Unit Of Work `_ patterns. So in Doctrine2 you would do
179
- the following :
182
+ `Data Mapper `_ and `Unit Of Work `_ patterns. The following example shows
183
+ how to save an entity with Doctrine2 :
180
184
181
185
.. code-block :: php
182
186
@@ -187,26 +191,27 @@ the following:
187
191
188
192
The "object manager" is a central object provided by Doctrine whose job
189
193
is to persist objects. You'll soon learn much more about this service.
190
- This paradigm shift lets us get rid of any base classes (e.g. the ``Post ``
191
- doesn't need to extend any base class) and static dependencies. Any object
194
+ This paradigm shift lets us get rid of any base classes (e.g. ``Post ``
195
+ doesn't need to extend a base class) and static dependencies. Any object
192
196
can be saved into a database for later retrieval. More than that, once persisted,
193
- an object is managed by the object manager, until the manager gets explicitly
194
- cleared. That means, that all object interactions happen in memory
195
- without ever going to the database until the ``$manager->flush() `` is
196
- called. Needless to say, that this kind of approach lets you worry about
197
- database and query optimizations even less, as all queries are as lazy
198
- as possible (i.e. their execution is deferred until the latest possible
199
- moment).
200
-
201
- A very important aspect of ActiveRecord is performance, or rather the difficulty
202
- in building a performant system. By using transactions and in-memory object
203
- change tracking, Doctrine2 minimizes the communication with the database,
204
- saving not only database execution time, but also expensive network communication.
197
+ an object is managed by the object manager until the manager is explicitly
198
+ cleared. This means all object interactions happen in memory,
199
+ without ever going to the database until ``$manager->flush() `` is
200
+ called. Needless to say, this provides an instant database and query
201
+ optimization compared to most other object persistence patterns, as all
202
+ queries are as lazy as possible (i.e. their execution is deferred until the
203
+ latest possible moment).
204
+
205
+ A very important aspect of the Active Record pattern is performance, or
206
+ rather, the *difficulty * in building a performant system. By using transactions
207
+ and in-memory object change tracking, Doctrine2 minimizes communication
208
+ with the database, saving not only database execution time, but also
209
+ expensive network communication.
205
210
206
211
Conclusion
207
212
----------
208
213
209
- Thanks to Doctrine2, The Model is now probably the simplest concept in
214
+ Thanks to Doctrine2, the Model is now probably the simplest concept in
210
215
Symfony2: it is in your complete control and not limited by persistence
211
216
specifics.
212
217
0 commit comments