44Databases and the Doctrine ORM
55==============================
66
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 `_.
169
1710.. note ::
1811
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.
3114
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.
3817
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+ -------------------
5520
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:
5923
60- .. configuration-block ::
61-
62- .. code-block :: yaml
24+ .. code-block :: terminal
6325
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
7227
73- .. code-block :: xml
28+ Configuring the Database
29+ ~~~~~~~~~~~~~~~~~~~~~~~~
7430
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 ``:
8433
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
9435
95- .. code-block :: php
36+ # .env
9637
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"
10740
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
11342
11443Now that Doctrine can connect to your database, the following command
11544can automatically generate an empty ``test_project `` database for you:
@@ -242,7 +171,7 @@ can automatically generate an empty ``test_project`` database for you:
242171 ));
243172
244173 Creating an Entity Class
245- ~~~~~~~~~~~~~~~~~~~~~~~~
174+ ------------------------
246175
247176Suppose you're building an application where products need to be displayed.
248177Without even thinking about Doctrine or databases, you already know that
@@ -280,7 +209,7 @@ just a simple PHP class.
280209.. _doctrine-adding-mapping :
281210
282211Add Mapping Information
283- ~~~~~~~~~~~~~~~~~~~~~~~
212+ -----------------------
284213
285214Doctrine allows you to work with databases in a much more interesting way
286215than just fetching rows of scalar data into an array. Instead, Doctrine
@@ -439,7 +368,7 @@ see the :ref:`doctrine-field-types` section.
439368 .. _doctrine-generating-getters-and-setters :
440369
441370Generating Getters and Setters
442- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371+ ------------------------------
443372
444373Even though Doctrine now knows how to persist a ``Product `` object to the
445374database, the class itself isn't really useful yet. Since ``Product `` is just
@@ -451,7 +380,7 @@ methods manually or with your own IDE.
451380.. _doctrine-creating-the-database-tables-schema :
452381
453382Creating the Database Tables/Schema
454- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383+ -----------------------------------
455384
456385You now have a usable ``Product `` class with mapping information so that
457386Doctrine 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
487416match the metadata you've specified.
488417
489418Persisting Objects to the Database
490- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
419+ ----------------------------------
491420
492421Now that you have mapped the ``Product `` entity to its corresponding ``product ``
493422table, 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.
580509 the "`DoctrineFixturesBundle `_" documentation.
581510
582511Fetching Objects from the Database
583- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512+ ----------------------------------
584513
585514Fetching an object back out of the database is even easier. For example,
586515suppose you've configured a route to display a specific ``Product `` based
@@ -675,7 +604,7 @@ to easily fetch objects based on multiple conditions::
675604 Symfony Profiler and see the exact queries that were executed.
676605
677606Updating an Object
678- ~~~~~~~~~~~~~~~~~~
607+ ------------------
679608
680609Once you've fetched an object from Doctrine, updating it is easy. Suppose
681610you 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
712641already managed.
713642
714643Deleting an Object
715- ~~~~~~~~~~~~~~~~~~
644+ ------------------
716645
717646Deleting an object is very similar, but requires a call to the ``remove() ``
718647method of the entity manager::
0 commit comments