Symfony – working with a database (Doctrine)

Work with the Symfony database. Work with ORM Doctrine.

All the same, with slight differences, is in the official guide:
https://symfony.com/doc/current/doctrine.html

Or in Russian:
https://symfony.ru/doc/current/doctrine.html

The official guide says more about something, something is skipped in view of what you yourself have to guess. Here below I tried to simplify and it may be more accurate to indicate what and where to put it.

The official guide is still worth reading, some things are chewed there.

Install tools for working with the database

composer require symfony / orm-pack
composer require –dev symfony / maker-bundle

DB creation

Create a database

php bin / console doctrine: database: create

Doctrine Teams

bin / console list doctrine

Install maker

composer require doctrine maker

Creating tables and entities in them

Create an entity

We answer questions by selecting the appropriate parameters for ourselves.

If you need to add a new field to the table in the future, then we use the same command.

We create (or update) a migration table for subsequent implementation in the database

php bin / console make: migration

A file will be listed in the console, something like:

Next: Review the new migration “src / Migrations / Version20200309102744.php”

It will have the parameters for creating the table.

Run the creation (or update) of the table in the database:

php bin / console doctrine: migrations: migrate

The same command should be run on production to update tables.

If you want to recreate entities, for example, in the case when adding is done manually through a file:

bin / console make: entity –regenerate

Controller

The controller is needed to store objects in the database.

php bin / console make: controller ProductController

Replace the contents with

namespace App Controller;

use Symfony Bundle FrameworkBundle Controller AbstractController;
use Symfony Component Routing Annotation Route;

use App Entity Product;
use Doctrine ORM EntityManagerInterface;
use Symfony Component HttpFoundation Response;

class ProductController extends AbstractController
{
/ **
* @Route (“/ product”, name = “create_product”)
* /
public function createProduct (): Response
{
// you can fetch the EntityManager via $ this-> getDoctrine ()
// or you can add an argument to the action: createProduct (EntityManagerInterface $ entityManager)
$ entityManager = $ this-> getDoctrine () -> getManager ();

$ product = new Product ();
$ product-> setName (‘Keyboard’);
$ product-> setPrice (1999);
$ product-> setDescription (‘Ergonomic and stylish!’);

// tell Doctrine you want to (eventually) save the Product (no queries yet)
$ entityManager-> persist ($ product);

// actually executes the queries (i.e. the INSERT query)
$ entityManager-> flush ();

return new Response (‘Saved new product with id’. $ product-> getId ());
}
}

Add the word product to the end of the address bar of the site and then open:

http: // localhost / symfony-svelte / public / product

This will create the first row of the database.

You can check through the console:

php bin / console doctrine: query: sql ‘SELECT * FROM product’

Getting an object from the database

In the same ProductController.php file, add:

/ **
* @Route (“/ product / {id}”, name = “product_show”)
* /

public function show($ id)
{
$ product = $ this->getDoctrine()
->getRepository(Product::class)
->find($ id);

if (!$ product) {
throw $ this->createNotFoundException(
‘No product found for id’.$ id
);
}

return new Response(‘Check out this great product:’.$ product->getName());

// or render a template
// in the template, print things with {{product.name}}
// return $ this-> render (‘product / show.html.twig’, [‘product’ => $product]);
}

And move on:

http: // localhost / symfony-svelte / public / product / 1

We will see the receipt of the above data. In this case, the Name field.

SensioFrameworkExtraBundle

To automate and simplify the process, install the framework:

composer require sensio / framework-extra-bundle

Simplify the method public function show (Product $ product)

/ **
* @Route (“/ product / {id}”, name = “product_show”)
* /

public function show(Product $ product)
{
return new Response(‘Check out this great product:’.$ product->getPrice());
}

And again, open the page:

http: // localhost / symfony-svelte / public / product / 1

We get the same as in the first embodiment.

More options here:
https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

Product update

/ **
* @Route (“/ product / edit / {id}”)
* /

public function update($ id)
{
$ entityManager = $ this->getDoctrine()->getManager();
$ product = $ entityManager->getRepository(Product::class)->find($ id);

if (!$ product) {
throw $ this->createNotFoundException(
‘No product found for id’.$ id
);
}

$ product->setName(‘New product name!’);
$ entityManager->flush();

return $ this->redirectToRoute(‘product_show’, [[
‘id’ => $ product->getId()
]);
}

http: // localhost / symfony-svelte / public / product / edit / 1

Delete

/ **
* @Route (“/ product / remove / {id}”)
* /

public function remove($ id)
{
$ entityManager = $ this->getDoctrine()->getManager();
$ product = $ entityManager->getRepository(Product::class)->find($ id);

if (!$ product) {
throw $ this->createNotFoundException(
‘No product found for id’.$ id
);
}

$ entityManager->remove($ product);
$ entityManager->flush();
}

Open:

http: // localhost / symfony-svelte / public / product / remove / 1

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *