@@ -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,21 @@ 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
- throw $this->createNotFoundException('No product found for id '.$id);
184
+ throw $this->createNotFoundException(
185
+ 'No product found for id '.$id
186
+ );
185
187
}
186
-
188
+
187
189
// ... do something, like pass the $product object into a template
188
190
}
189
191
@@ -192,22 +194,24 @@ Updating an Object
192
194
193
195
Once you've fetched an object from Propel, updating it is easy. Suppose you
194
196
have a route that maps a product id to an update action in a controller::
195
-
197
+
196
198
// ...
197
199
use Acme\StoreBundle\Model\ProductQuery;
198
-
200
+
199
201
public function updateAction($id)
200
202
{
201
203
$product = ProductQuery::create()
202
204
->findPk($id);
203
-
205
+
204
206
if (!$product) {
205
- throw $this->createNotFoundException('No product found for id '.$id);
207
+ throw $this->createNotFoundException(
208
+ 'No product found for id '.$id
209
+ );
206
210
}
207
-
211
+
208
212
$product->setName('New product name!');
209
213
$product->save();
210
-
214
+
211
215
return $this->redirect($this->generateUrl('homepage'));
212
216
}
213
217
@@ -227,12 +231,12 @@ method on the object::
227
231
228
232
Querying for Objects
229
233
--------------------
230
-
234
+
231
235
Propel provides generated ``Query `` classes to run both basic and complex queries
232
236
without any work::
233
-
237
+
234
238
\Acme\StoreBundle\Model\ProductQuery::create()->findPk($id);
235
-
239
+
236
240
\Acme\StoreBundle\Model\ProductQuery::create()
237
241
->filterByName('Foo')
238
242
->findOne();
@@ -287,13 +291,13 @@ Start by adding the ``category`` definition in your ``schema.xml``:
287
291
<column name =" name" type =" varchar" primaryString =" true" size =" 100" />
288
292
<column name =" price" type =" decimal" />
289
293
<column name =" description" type =" longvarchar" />
290
-
294
+
291
295
<column name =" category_id" type =" integer" />
292
296
<foreign-key foreignTable =" category" >
293
297
<reference local =" category_id" foreign =" id" />
294
298
</foreign-key >
295
299
</table >
296
-
300
+
297
301
<table name =" category" >
298
302
<column name =" id" type =" integer" required =" true" primaryKey =" true" autoIncrement =" true" />
299
303
<column name =" name" type =" varchar" primaryString =" true" size =" 100" />
@@ -326,23 +330,23 @@ Now, let's see the code in action. Imagine you're inside a controller::
326
330
use Acme\StoreBundle\Model\Category;
327
331
use Acme\StoreBundle\Model\Product;
328
332
use Symfony\Component\HttpFoundation\Response;
329
-
333
+
330
334
class DefaultController extends Controller
331
335
{
332
336
public function createProductAction()
333
337
{
334
338
$category = new Category();
335
339
$category->setName('Main Products');
336
-
340
+
337
341
$product = new Product();
338
342
$product->setName('Foo');
339
343
$product->setPrice(19.99);
340
344
// relate this product to the category
341
345
$product->setCategory($category);
342
-
346
+
343
347
// save the whole
344
348
$product->save();
345
-
349
+
346
350
return new Response(
347
351
'Created product id: '.$product->getId().' and category id: '.$category->getId()
348
352
);
@@ -363,15 +367,15 @@ before. First, fetch a ``$product`` object and then access its related
363
367
364
368
// ...
365
369
use Acme\StoreBundle\Model\ProductQuery;
366
-
370
+
367
371
public function showAction($id)
368
372
{
369
373
$product = ProductQuery::create()
370
374
->joinWithCategory()
371
375
->findPk($id);
372
-
376
+
373
377
$categoryName = $product->getCategory()->getName();
374
-
378
+
375
379
// ...
376
380
}
377
381
@@ -395,7 +399,7 @@ inserted, updated, deleted, etc).
395
399
To add a hook, just add a new method to the object class::
396
400
397
401
// src/Acme/StoreBundle/Model/Product.php
398
-
402
+
399
403
// ...
400
404
class Product extends BaseProduct
401
405
{
0 commit comments