Working with Doctrine in Symfony framework. Basics (Lesson 10. Doctrine)

So, we already know theoretically (here you can read), why Doctrine, what advantages it provides compared to "bare" sql queries. Now we move on to practice.

As I advised in the previous lesson, the easiest way is to create an ORM based on an existing database. You will need only two commands:

php app/console doctrine:mapping:import Bundle_Name config_format

This way we will create a configuration for model building. The format can be different, for example, yml, xml, etc. After that we create the ORM:

php app/console doctrine:generate:entities Bundle_Name

At this point, creating the ORM can be considered complete. And if the tables were related, all the necessary relationships were instantly created in the model. You can start working with the database.

 

Note. As a result of the first command a database configuration is created (by default in src/Path_to_bundle/Resources/config/doctrine). Sometimes you have to edit it, for example, to be able to set default values, more on that in one of the next notes.

The second command generates classes called mapping along with getter and setter methods. (By default, in src/Path_to_bundle/Entity). These classes represent database tables.

If you don't have a ready-made database and want to create it, run

php app/console doctrine:database:create

Deleting a database is possible with the command

php app/console doctrine:database:drop --force

Next

php app/console doctrine:generate:entity

This will invoke an interactive generator. Following its instructions, you will create a model (mapping). Based on this model, you can create tables in the database. After completing work with the interactive generator, create the model, as mentioned earlier

php app/console doctrine:generate:entities Bundle_Name

Create tables in the database using mapping:

php app/console doctrine:schema:update --force

We are now moving directly to working with the database using Doctrine. This note is introductory, so for now we will manage without DQL queries.

 

Like SELECT To make a simple selection from a single database table, just write in the controller:

$product = $this->getDoctrine()
->getRepository('Bundle:table')
->find($id);

The find($id) method selects by id (by primary key). In addition to this, there are methods

findOneById($id),

findAll() - get all records in the table,

findBy(array('field'=>'value')) - selection with a condition, for example,

 

$products = $repository->findBy(
  array('name' => 'foo'),
  array('price' => 'ASC') );

As you probably guessed, the type $products is our generated model (mapping). (That which is in src/Path_to_bundle/Entity)

 

Like INSERT/UPDATE It is very convenient that when using Doctrine the difference between adding a record and updating an existing one is minimal. It all depends on which object you are updating - the one for which a SELECT was made before, or not.

Let's start with adding (INSERT) to the database:

//Get the DB Manager - Entity Manager
$em = $this->getDoctrine()->getManager();

//Create an instance of the model $product = new Product;

//Set the value of the fields $product->setValue('value'); ...

//Pass the model object to the manager $em->persist($product)

//Add a record to the table $em->flush();

Updating (UPDATE) is different in that you need to first select a record for update. And that's it!

//Get the DB Manager
$em = $this->getDoctrine()-&-gt;getManager();

//Select the record to update - this line is the only difference $product = $em->getRepository('AcmeStoreBundle:Product')->find($id);

//Set the value of the fields $product->setValue('value'); ...

//Pass the model object to the manager $em->persist($product)

//Add a record to the table $em->flush();

Like DELETE

To delete, just call remove() instead of persist():

$em->remove($product);
$em->flush();

That's it for now. We have learned how to perform basic actions with the database in Symfony framework using Doctrine.