4
4
Databases and Propel
5
5
====================
6
6
7
- Let's face it, one of the most common and challenging tasks for any application
7
+ One of the most common and challenging tasks for any application
8
8
involves persisting and reading information to and from a database. Symfony2
9
9
does not come integrated with any ORMs but the Propel integration is easy.
10
10
To get started, read `Working With Symfony2 `_.
@@ -18,8 +18,8 @@ persist it to the database and fetch it back out.
18
18
.. sidebar :: Code along with the example
19
19
20
20
If you want to follow along with the example in this chapter, create an
21
- ``AcmeStoreBundle `` via:
22
-
21
+ ``AcmeStoreBundle `` via:
22
+
23
23
.. code-block :: bash
24
24
25
25
$ php app/console generate:bundle --namespace=Acme/StoreBundle
@@ -171,19 +171,19 @@ Fetching Objects from the Database
171
171
Fetching an object back from the database is even easier. For example, suppose
172
172
you've configured a route to display a specific ``Product `` based on its ``id ``
173
173
value::
174
-
174
+
175
175
// ...
176
176
use Acme\StoreBundle\Model\ProductQuery;
177
-
177
+
178
178
public function showAction($id)
179
179
{
180
180
$product = ProductQuery::create()
181
181
->findPk($id);
182
-
182
+
183
183
if (!$product) {
184
184
throw $this->createNotFoundException('No product found for id '.$id);
185
185
}
186
-
186
+
187
187
// ... do something, like pass the $product object into a template
188
188
}
189
189
@@ -192,22 +192,22 @@ Updating an Object
192
192
193
193
Once you've fetched an object from Propel, updating it is easy. Suppose you
194
194
have a route that maps a product id to an update action in a controller::
195
-
195
+
196
196
// ...
197
197
use Acme\StoreBundle\Model\ProductQuery;
198
-
198
+
199
199
public function updateAction($id)
200
200
{
201
201
$product = ProductQuery::create()
202
202
->findPk($id);
203
-
203
+
204
204
if (!$product) {
205
205
throw $this->createNotFoundException('No product found for id '.$id);
206
206
}
207
-
207
+
208
208
$product->setName('New product name!');
209
209
$product->save();
210
-
210
+
211
211
return $this->redirect($this->generateUrl('homepage'));
212
212
}
213
213
@@ -227,12 +227,12 @@ method on the object::
227
227
228
228
Querying for Objects
229
229
--------------------
230
-
230
+
231
231
Propel provides generated ``Query `` classes to run both basic and complex queries
232
232
without any work::
233
-
233
+
234
234
\Acme\StoreBundle\Model\ProductQuery::create()->findPk($id);
235
-
235
+
236
236
\Acme\StoreBundle\Model\ProductQuery::create()
237
237
->filterByName('Foo')
238
238
->findOne();
@@ -287,13 +287,13 @@ Start by adding the ``category`` definition in your ``schema.xml``:
287
287
<column name =" name" type =" varchar" primaryString =" true" size =" 100" />
288
288
<column name =" price" type =" decimal" />
289
289
<column name =" description" type =" longvarchar" />
290
-
290
+
291
291
<column name =" category_id" type =" integer" />
292
292
<foreign-key foreignTable =" category" >
293
293
<reference local =" category_id" foreign =" id" />
294
294
</foreign-key >
295
295
</table >
296
-
296
+
297
297
<table name =" category" >
298
298
<column name =" id" type =" integer" required =" true" primaryKey =" true" autoIncrement =" true" />
299
299
<column name =" name" type =" varchar" primaryString =" true" size =" 100" />
@@ -320,29 +320,29 @@ Your database has been updated, you can continue to write your application.
320
320
Saving Related Objects
321
321
~~~~~~~~~~~~~~~~~~~~~~
322
322
323
- Now, let's see the code in action. Imagine you're inside a controller::
323
+ Now, try the code in action. Imagine you're inside a controller::
324
324
325
325
// ...
326
326
use Acme\StoreBundle\Model\Category;
327
327
use Acme\StoreBundle\Model\Product;
328
328
use Symfony\Component\HttpFoundation\Response;
329
-
329
+
330
330
class DefaultController extends Controller
331
331
{
332
332
public function createProductAction()
333
333
{
334
334
$category = new Category();
335
335
$category->setName('Main Products');
336
-
336
+
337
337
$product = new Product();
338
338
$product->setName('Foo');
339
339
$product->setPrice(19.99);
340
340
// relate this product to the category
341
341
$product->setCategory($category);
342
-
342
+
343
343
// save the whole
344
344
$product->save();
345
-
345
+
346
346
return new Response(
347
347
'Created product id: '.$product->getId().' and category id: '.$category->getId()
348
348
);
@@ -363,15 +363,15 @@ before. First, fetch a ``$product`` object and then access its related
363
363
364
364
// ...
365
365
use Acme\StoreBundle\Model\ProductQuery;
366
-
366
+
367
367
public function showAction($id)
368
368
{
369
369
$product = ProductQuery::create()
370
370
->joinWithCategory()
371
371
->findPk($id);
372
-
372
+
373
373
$categoryName = $product->getCategory()->getName();
374
-
374
+
375
375
// ...
376
376
}
377
377
@@ -395,7 +395,7 @@ inserted, updated, deleted, etc).
395
395
To add a hook, just add a new method to the object class::
396
396
397
397
// src/Acme/StoreBundle/Model/Product.php
398
-
398
+
399
399
// ...
400
400
class Product extends BaseProduct
401
401
{
@@ -429,8 +429,8 @@ Commands
429
429
430
430
You should read the dedicated section for `Propel commands in Symfony2 `_.
431
431
432
- .. _`Working With Symfony2` : http://www. propelorm.org/cookbook/symfony2/working-with-symfony2.html#installation
433
- .. _`Working With Symfony2 - Configuration` : http://www. propelorm.org/cookbook/symfony2/working-with-symfony2.html#configuration
434
- .. _`Relationships` : http://www. propelorm.org/documentation/04-relationships.html
435
- .. _`Behaviors reference section` : http://www. propelorm.org/documentation/#behaviors_reference
436
- .. _`Propel commands in Symfony2` : http://www. propelorm.org/cookbook/symfony2/working-with-symfony2#the_commands
432
+ .. _`Working With Symfony2` : http://propelorm.org/cookbook/symfony2/working-with-symfony2.html#installation
433
+ .. _`Working With Symfony2 - Configuration` : http://propelorm.org/cookbook/symfony2/working-with-symfony2.html#configuration
434
+ .. _`Relationships` : http://propelorm.org/documentation/04-relationships.html
435
+ .. _`Behaviors reference section` : http://propelorm.org/documentation/#behaviors_reference
436
+ .. _`Propel commands in Symfony2` : http://propelorm.org/cookbook/symfony2/working-with-symfony2#the_commands
0 commit comments