4
4
Databases and the Doctrine ORM
5
5
==============================
6
6
7
- One of the most common and challenging tasks for any application
8
- involves persisting and reading information to and from a database. Although
9
- the Symfony Framework doesn't integrate any component to work with databases,
10
- it provides tight integration with a third-party library called `Doctrine `_.
11
- Doctrine's sole goal is to give you powerful tools to make database interactions
12
- easy and flexible.
13
-
14
- In this chapter, you'll learn how to start leveraging Doctrine in your Symfony projects
15
- to give you rich database interactions.
7
+ Symfony doesn't provide a component to work with the database, but it *does * provide
8
+ tight integration with a third-party library called `Doctrine `_.
16
9
17
10
.. note ::
18
11
19
- Doctrine is totally decoupled from Symfony and using it is optional.
20
- This chapter is all about the Doctrine ORM, which aims to let you map
21
- objects to a relational database (such as *MySQL *, *PostgreSQL * or
22
- *Microsoft SQL *). If you prefer to use raw database queries, this is
23
- easy, and explained in the ":doc: `/doctrine/dbal `" article.
24
-
25
- You can also persist data to `MongoDB `_ using Doctrine ODM library. For
26
- more information, read the "`DoctrineMongoDBBundle `_"
27
- documentation.
28
-
29
- A Simple Example: A Product
30
- ---------------------------
12
+ This article is all about using the Doctrine ORM. If you prefer to use raw
13
+ database queries, see the ":doc: `/doctrine/dbal `" article instead.
31
14
32
- The easiest way to understand how Doctrine works is to see it in action.
33
- In this section, you'll configure your database, create a ``Product `` object,
34
- persist it to the database and fetch it back out.
35
-
36
- Configuring the Database
37
- ~~~~~~~~~~~~~~~~~~~~~~~~
15
+ You can also persist data to `MongoDB `_ using Doctrine ODM library. See the
16
+ "`DoctrineMongoDBBundle `_" documentation.
38
17
39
- Before you really begin, you'll need to configure your database connection
40
- information. By convention, this information is usually configured in an
41
- ``app/config/parameters.yml `` file:
42
-
43
- .. code-block :: yaml
44
-
45
- # app/config/parameters.yml
46
- parameters :
47
- database_host : localhost
48
- database_name : test_project
49
- database_user : root
50
- database_password : password
51
-
52
- # ...
53
-
54
- .. note ::
18
+ Installing Doctrine
19
+ -------------------
55
20
56
- Defining the configuration via ``parameters.yml `` is just a convention.
57
- The parameters defined in that file are referenced by the main configuration
58
- file when setting up Doctrine:
21
+ First, install Doctrine, as well as the MakerBundle, which will help generate some
22
+ code:
59
23
60
- .. configuration-block ::
61
-
62
- .. code-block :: yaml
24
+ .. code-block :: terminal
63
25
64
- # app/config/config.yml
65
- doctrine :
66
- dbal :
67
- driver : pdo_mysql
68
- host : ' %database_host%'
69
- dbname : ' %database_name%'
70
- user : ' %database_user%'
71
- password : ' %database_password%'
26
+ composer require orm maker
72
27
73
- .. code-block :: xml
28
+ Configuring the Database
29
+ ~~~~~~~~~~~~~~~~~~~~~~~~
74
30
75
- <!-- app/config/config.xml -->
76
- <?xml version =" 1.0" encoding =" UTF-8" ?>
77
- <container xmlns =" http://symfony.com/schema/dic/services"
78
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
79
- xmlns : doctrine =" http://symfony.com/schema/dic/doctrine"
80
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
81
- http://symfony.com/schema/dic/services/services-1.0.xsd
82
- http://symfony.com/schema/dic/doctrine
83
- http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd" >
31
+ The database connection information is stored as an environment variable called
32
+ ``DATABASE_URL ``. For development, you can find and customize this inside ``.env ``:
84
33
85
- <doctrine : config >
86
- <doctrine : dbal
87
- driver =" pdo_mysql"
88
- host =" %database_host%"
89
- dbname =" %database_name%"
90
- user =" %database_user%"
91
- password =" %database_password%" />
92
- </doctrine : config >
93
- </container >
34
+ .. code-block :: text
94
35
95
- .. code-block :: php
36
+ # .env
96
37
97
- // app/config/config.php
98
- $container->loadFromExtension('doctrine', array(
99
- 'dbal' => array(
100
- 'driver' => 'pdo_mysql',
101
- 'host' => '%database_host%',
102
- 'dbname' => '%database_name%',
103
- 'user' => '%database_user%',
104
- 'password' => '%database_password%',
105
- ),
106
- ));
38
+ # customize this line!
39
+ DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?charset=utf8mb4&serverVersion=5.7"
107
40
108
- By separating the database information into a separate file, you can
109
- easily keep different versions of the file on each server. You can also
110
- easily store database configuration (or any sensitive information) outside
111
- of your project, like inside your Apache configuration, for example. For
112
- more information, see :doc: `/configuration/external_parameters `.
41
+ ----> HERE
113
42
114
43
Now that Doctrine can connect to your database, the following command
115
44
can automatically generate an empty ``test_project `` database for you:
@@ -242,7 +171,7 @@ can automatically generate an empty ``test_project`` database for you:
242
171
));
243
172
244
173
Creating an Entity Class
245
- ~~~~~~~~~~~~~~~~~~~~~~~~
174
+ ------------------------
246
175
247
176
Suppose you're building an application where products need to be displayed.
248
177
Without even thinking about Doctrine or databases, you already know that
@@ -280,7 +209,7 @@ just a simple PHP class.
280
209
.. _doctrine-adding-mapping :
281
210
282
211
Add Mapping Information
283
- ~~~~~~~~~~~~~~~~~~~~~~~
212
+ -----------------------
284
213
285
214
Doctrine allows you to work with databases in a much more interesting way
286
215
than just fetching rows of scalar data into an array. Instead, Doctrine
@@ -439,7 +368,7 @@ see the :ref:`doctrine-field-types` section.
439
368
.. _doctrine-generating-getters-and-setters :
440
369
441
370
Generating Getters and Setters
442
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371
+ ------------------------------
443
372
444
373
Even though Doctrine now knows how to persist a ``Product `` object to the
445
374
database, the class itself isn't really useful yet. Since ``Product `` is just
@@ -451,7 +380,7 @@ methods manually or with your own IDE.
451
380
.. _doctrine-creating-the-database-tables-schema :
452
381
453
382
Creating the Database Tables/Schema
454
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383
+ -----------------------------------
455
384
456
385
You now have a usable ``Product `` class with mapping information so that
457
386
Doctrine knows exactly how to persist it. Of course, you don't yet have the
@@ -487,7 +416,7 @@ Your database now has a fully-functional ``product`` table with columns that
487
416
match the metadata you've specified.
488
417
489
418
Persisting Objects to the Database
490
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
419
+ ----------------------------------
491
420
492
421
Now that you have mapped the ``Product `` entity to its corresponding ``product ``
493
422
table, you're ready to persist ``Product `` objects to the database. From inside
@@ -580,7 +509,7 @@ issue an ``UPDATE`` query if the entity already exists in the database.
580
509
the "`DoctrineFixturesBundle `_" documentation.
581
510
582
511
Fetching Objects from the Database
583
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512
+ ----------------------------------
584
513
585
514
Fetching an object back out of the database is even easier. For example,
586
515
suppose you've configured a route to display a specific ``Product `` based
@@ -675,7 +604,7 @@ to easily fetch objects based on multiple conditions::
675
604
Symfony Profiler and see the exact queries that were executed.
676
605
677
606
Updating an Object
678
- ~~~~~~~~~~~~~~~~~~
607
+ ------------------
679
608
680
609
Once you've fetched an object from Doctrine, updating it is easy. Suppose
681
610
you have a route that maps a product id to an update action in a controller::
@@ -712,7 +641,7 @@ In this case, since you fetched the ``$product`` object from Doctrine, it's
712
641
already managed.
713
642
714
643
Deleting an Object
715
- ~~~~~~~~~~~~~~~~~~
644
+ ------------------
716
645
717
646
Deleting an object is very similar, but requires a call to the ``remove() ``
718
647
method of the entity manager::
0 commit comments