Databases and Doctrine

Databases are the backbone of almost every web application. Every time we need to store or retrieve data, we do so with the help of databases. The challenge in the modern OOP world is to abstract the database so that our PHP code is database agnostic. MySQL is probably the most known database in the PHP world. PHP itself has a great support for working with MySQL, whether it is via the mysqli_* extension or via PDO. However, both approaches are MySQL specific, to o close to database. Doctrine solves this problem by introducing a level of abstraction, enabling us to work with PHP objects that represent tables, rows, and their relations in MySQL.

Doctrine is completely decoupled from Symfony, so using it is completely optional. The great thing about it, however, is that the Symfony console provides great auto-generated CRUD based on Doctrine ORM, as we saw in previous examples when creating Customer entity.

As soon as we created the project, Symfony provided us with an auto-generated app/config/parameters.yml file. This is the file in which we, among other things, provide database access information as shown in the following example:

parameters:
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: mysql

Once we configure proper parameters, we can use console generation features.

It is worth noting that parameters within this file are merely a convention, as app/config/config.yml is pulling them under doctrine dbal configuration like the one shown here:

doctrine:
dbal:
  driver:   pdo_mysql
  host:     "%database_host%"
  port:     "%database_port%"
  dbname:   "%database_name%"
  user:     "%database_user%"
  password: "%database_password%"
  charset:  UTF8

The Symfony console tool allows us to drop and create a database based on this config, which comes in handy during development, as shown in the following code block:

php bin/console doctrine:database:drop --force
php bin/console doctrine:database:create

We saw previously how the console tool enables us to create entities and their mapping into database tables. This will suffice for our needs throughout this book. Once we have them created, we need to be able to perform CRUD operations on them. If we gloss over the auto-generated CRUD controller src/AppBundle/Controller/CustomerController.php file, we can the CRUD related code as follows:

// Fetch all entities
$customers = $em->getRepository('AppBundle:Customer')->findAll();

// Persist single entity (existing or new)
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();

// Delete single entity
$em = $this->getDoctrine()->getManager();
$em->remove($customer);
$em->flush();

There is a lot more to be said about Doctrine, which is far out of the scope of this book. More information can be found at the official page (http://www.doctrine-project.org).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.142.146