@@ -34,9 +34,9 @@ persist it to the database and fetch it back out.
34
34
35
35
If you want to follow along with the example in this chapter, create
36
36
an ``AcmeStoreBundle `` via:
37
-
37
+
38
38
.. code-block :: bash
39
-
39
+
40
40
$ php app/console generate:bundle --namespace=Acme/StoreBundle
41
41
42
42
Configuring the Database
@@ -61,9 +61,9 @@ information. By convention, this information is usually configured in an
61
61
Defining the configuration via ``parameters.ini `` is just a convention.
62
62
The parameters defined in that file are referenced by the main configuration
63
63
file when setting up Doctrine:
64
-
64
+
65
65
.. code-block :: yaml
66
-
66
+
67
67
# app/config/config.yml
68
68
doctrine :
69
69
dbal :
@@ -72,13 +72,20 @@ information. By convention, this information is usually configured in an
72
72
dbname : %database_name%
73
73
user : %database_user%
74
74
password : %database_password%
75
-
75
+
76
76
By separating the database information into a separate file, you can
77
77
easily keep different versions of the file on each server. You can also
78
78
easily store database configuration (or any sensitive information) outside
79
79
of your project, like inside your Apache configuration, for example. For
80
80
more information, see :doc: `/cookbook/configuration/external_parameters `.
81
81
82
+ Now that Doctrine knows about your database, you can have it create the database
83
+ for you:
84
+
85
+ .. code-block :: bash
86
+
87
+ $ php app/console doctrine:database:create
88
+
82
89
.. sidebar :: Setting Up The Database
83
90
84
91
One mistake even seasoned developers make when starting a Symfony2 project
@@ -100,17 +107,10 @@ information. By convention, this information is usually configured in an
100
107
your configuration file (typically ``my.cnf ``):
101
108
102
109
.. code-block :: ini
103
-
110
+
104
111
[mysqld]
105
112
collation-server = utf8_general_ci
106
- character-set-server = utf8
107
-
108
- Now that Doctrine knows about your database, you can have it create the database
109
- for you:
110
-
111
- .. code-block :: bash
112
-
113
- $ php app/console doctrine:database:create
113
+ character-set-server = utf8
114
114
115
115
Creating an Entity Class
116
116
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -141,9 +141,9 @@ just a simple PHP class.
141
141
142
142
Once you learn the concepts behind Doctrine, you can have Doctrine create
143
143
this entity class for you:
144
-
144
+
145
145
.. code-block :: bash
146
-
146
+
147
147
$ php app/console doctrine:generate:entity --entity=" AcmeStoreBundle:Product" --fields=" name:string(255) price:float description:text"
148
148
149
149
.. index ::
@@ -440,9 +440,9 @@ Let's walk through this example:
440
440
In fact, since Doctrine is aware of all your managed entities, when you
441
441
call the ``flush() `` method, it calculates an overall changeset and executes
442
442
the most efficient query/queries possible. For example, if you persist a
443
- total of 100 ``Product `` objects and then subsequently call ``flush() ``,
444
- Doctrine will create a *single * prepared statement and re-use it for each
445
- insert. This pattern is called *Unit of Work *, and it's used because it's
443
+ total of 100 ``Product `` objects and then subsequently call ``flush() ``,
444
+ Doctrine will create a *single * prepared statement and re-use it for each
445
+ insert. This pattern is called *Unit of Work *, and it's used because it's
446
446
fast and efficient.
447
447
448
448
When creating or updating objects, the workflow is always the same. In the
@@ -467,7 +467,7 @@ on its ``id`` value::
467
467
$product = $this->getDoctrine()
468
468
->getRepository('AcmeStoreBundle:Product')
469
469
->find($id);
470
-
470
+
471
471
if (!$product) {
472
472
throw $this->createNotFoundException('No product found for id '.$id);
473
473
}
@@ -589,7 +589,7 @@ You've already seen how the repository object allows you to run basic queries
589
589
without any work::
590
590
591
591
$repository->find($id);
592
-
592
+
593
593
$repository->findOneByName('Foo');
594
594
595
595
Of course, Doctrine also allows you to write more complex queries using the
@@ -611,7 +611,7 @@ a controller, do the following::
611
611
$query = $em->createQuery(
612
612
'SELECT p FROM AcmeStoreBundle:Product p WHERE p.price > :price ORDER BY p.price ASC'
613
613
)->setParameter('price', '19.99');
614
-
614
+
615
615
$products = $query->getResult();
616
616
617
617
If you're comfortable with SQL, then DQL should feel very natural. The biggest
@@ -632,10 +632,10 @@ for just one object, you can use the ``getSingleResult()`` method instead::
632
632
need to wrap it in a try-catch block and ensure that only one result is
633
633
returned (if you're querying on something that could feasibly return
634
634
more than one result)::
635
-
635
+
636
636
$query = $em->createQuery('SELECT ...')
637
637
->setMaxResults(1);
638
-
638
+
639
639
try {
640
640
$product = $query->getSingleResult();
641
641
} catch (\Doctrine\Orm\NoResultException $e) {
@@ -653,7 +653,7 @@ covered later), group, etc. For more information, see the official Doctrine
653
653
Take note of the ``setParameter() `` method. When working with Doctrine,
654
654
it's always a good idea to set any external values as "placeholders",
655
655
which was done in the above query:
656
-
656
+
657
657
.. code-block :: text
658
658
659
659
... WHERE p.price > :price ...
@@ -689,7 +689,7 @@ type the method names. From inside a controller::
689
689
->setParameter('price', '19.99')
690
690
->orderBy('p.price', 'ASC')
691
691
->getQuery();
692
-
692
+
693
693
$products = $query->getResult();
694
694
695
695
The ``QueryBuilder `` object contains every method necessary to build your
@@ -824,16 +824,16 @@ To relate the ``Category`` and ``Product`` entities, start by creating a
824
824
825
825
// ...
826
826
use Doctrine\Common\Collections\ArrayCollection;
827
-
827
+
828
828
class Category
829
829
{
830
830
// ...
831
-
831
+
832
832
/**
833
833
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
834
834
*/
835
835
protected $products;
836
-
836
+
837
837
public function __construct()
838
838
{
839
839
$this->products = new ArrayCollection();
@@ -870,7 +870,7 @@ makes sense in the application for each ``Category`` to hold an array of
870
870
.. tip ::
871
871
872
872
The targetEntity value in the decorator used above can reference any entity
873
- with a valid namespace, not just entities defined in the same class. To
873
+ with a valid namespace, not just entities defined in the same class. To
874
874
relate to an entity defined in a different class or bundle, enter a full
875
875
namespace as the targetEntity.
876
876
@@ -884,11 +884,11 @@ object, you'll want to add a ``$category`` property to the ``Product`` class:
884
884
// src/Acme/StoreBundle/Entity/Product.php
885
885
886
886
// ...
887
-
887
+
888
888
class Product
889
889
{
890
890
// ...
891
-
891
+
892
892
/**
893
893
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
894
894
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
@@ -970,18 +970,18 @@ Now, let's see the code in action. Imagine you're inside a controller::
970
970
{
971
971
$category = new Category();
972
972
$category->setName('Main Products');
973
-
973
+
974
974
$product = new Product();
975
975
$product->setName('Foo');
976
976
$product->setPrice(19.99);
977
977
// relate this product to the category
978
978
$product->setCategory($category);
979
-
979
+
980
980
$em = $this->getDoctrine()->getEntityManager();
981
981
$em->persist($category);
982
982
$em->persist($product);
983
983
$em->flush();
984
-
984
+
985
985
return new Response(
986
986
'Created product id: '.$product->getId().' and category id: '.$category->getId()
987
987
);
@@ -1007,7 +1007,7 @@ did before. First, fetch a ``$product`` object and then access its related
1007
1007
->find($id);
1008
1008
1009
1009
$categoryName = $product->getCategory()->getName();
1010
-
1010
+
1011
1011
// ...
1012
1012
}
1013
1013
@@ -1034,7 +1034,7 @@ You can also query in the other direction::
1034
1034
->find($id);
1035
1035
1036
1036
$products = $category->getProducts();
1037
-
1037
+
1038
1038
// ...
1039
1039
}
1040
1040
@@ -1049,7 +1049,7 @@ to the given ``Category`` object via their ``category_id`` value.
1049
1049
This "lazy loading" is possible because, when necessary, Doctrine returns
1050
1050
a "proxy" object in place of the true object. Look again at the above
1051
1051
example::
1052
-
1052
+
1053
1053
$product = $this->getDoctrine()
1054
1054
->getRepository('AcmeStoreBundle:Product')
1055
1055
->find($id);
@@ -1115,9 +1115,9 @@ object and its related ``Category`` with just one query::
1115
1115
->findOneByIdJoinedToCategory($id);
1116
1116
1117
1117
$category = $product->getCategory();
1118
-
1118
+
1119
1119
// ...
1120
- }
1120
+ }
1121
1121
1122
1122
More Information on Associations
1123
1123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1233,7 +1233,7 @@ in general, see Doctrine's `Lifecycle Events documentation`_
1233
1233
callbacks should be simple methods that are concerned with internally
1234
1234
transforming data in the entity (e.g. setting a created/updated field,
1235
1235
generating a slug value).
1236
-
1236
+
1237
1237
If you need to do some heavier lifting - like perform logging or send
1238
1238
an email - you should register an external class as an event listener
1239
1239
or subscriber and give it access to whatever resources you need. For
@@ -1300,11 +1300,11 @@ and ``nullable``. Take a few examples:
1300
1300
/**
1301
1301
* A string field with length 255 that cannot be null
1302
1302
* (reflecting the default values for the "type", "length" and *nullable* options)
1303
- *
1303
+ *
1304
1304
* @ORM\Column()
1305
1305
*/
1306
1306
protected $name;
1307
-
1307
+
1308
1308
/**
1309
1309
* A string field of length 150 that persists to an "email_address" column
1310
1310
* and has a unique index.
@@ -1364,9 +1364,9 @@ Some notable or interesting tasks include:
1364
1364
* ``doctrine:ensure-production-settings `` - checks to see if the current
1365
1365
environment is configured efficiently for production. This should always
1366
1366
be run in the ``prod `` environment:
1367
-
1367
+
1368
1368
.. code-block :: bash
1369
-
1369
+
1370
1370
$ php app/console doctrine:ensure-production-settings --env=prod
1371
1371
1372
1372
* ``doctrine:mapping:import `` - allows Doctrine to introspect an existing
0 commit comments