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