Posts

Showing posts with the label Doctrine ORM

Symfony Commands for Loading and Purging Fixtures

Doctrine already has a command to load fixtures, symfony console doctrine:fixtures:load , but does not take into account the reset of sequences and there is no command just to purge fixtures. The article describes Symfony commands: Purge Database, Restart Sequences (PostgreSQL) and Load Fixtures Purge Database Without Loading Fixtures Purge Database, Restart Sequences (PostgreSQL) and Load Fixtures // src/Command/LoadFixturesCommand.php <?php namespace App\Command; use Doctrine\DBAL\Exception; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\ExceptionInterface; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand( name: 'app:fixtures:load', description: 'Purg...

How to Use doctrine:migrations:rollup Command

During the development process, a large number of migrations are created, but rollbacks are not often used or not used at all. Instead of creating another migration, it's better to create one single migration that will include all past database schema changes. That's what the doctrine:migrations:rollup command is for. The doctrine:migrations:rollup command does the following: Check that there is only one migration in the migrations directory Delete executed migrations from the doctrine_migration_versions table Will add a single migration to the doctrine_migration_versions table, but will not execute it A single migration will not be executed because it is expected that all previous migrations were executed earlier, and the migration itself contains the up-to-date database schema. Steps to run doctrine:migrations:rollup command: DEV environment Dump database data using pg_dumpall -a -U USERNAME > dump.sql command Drop all tables and sequences: dr...

Doctrine fetch: EXTRA_LAZY, LAZY, EAGER

Fetch means whether a related entity or collection will be additionally fetched. For example, is it necessary to get the Photo entity along with getting the Product entity? There are several fetch strategies: EXTRA_LAZY - related collection will not be fetched until the collection method is called, except for these methods contains($entity) , containsKey($key) , count() , get($key) , slice($offset, $length = null) , add($entity) . Adding an entity in this way will also not cause the collection to be fetched: $collection[] = $entity LAZY - related entity or collection will not be fetched until the entity property or collection method is called EAGER - related entity or collection will always be fetched The EXTRA_LAZY strategy is used for collections, LAZY and EAGER for entities. By default, all relation types use the LAZY strategy. Rules for setting strategies: For relation OneToOne fit EAGER For relation OneToMany , fit EXTRA_LAZY For relation ManyToOne , fi...

Doctrine Entity Relations

Image
An entity is an identifiable object that is stored in a database. Relation means belonging of one entity to another. For example, a relation between the Order and DeliveryAddress entities means that the order has a delivery address or the delivery address belongs to the order. There are several relation types between entities: OneToOne, OneToMany, ManyToOne, ManyToMany. OneToOne OneToOne means that only one entity belongs to one entity. For example, one order has only one delivery address. The OneToOne relation makes it possible to get the DeliveryAddress entity from the Order entity. #[ORM\Entity(repositoryClass: OrderRepository::class)] #[ORM\Table(name: '`order`')] class Order { #[ORM\OneToOne()] private ?DeliveryAddress $deliveryAddress = null; } #[ORM\Entity(repositoryClass: DeliveryAddressRepository::class)] class DeliveryAddress { // relation is set on the side of the Order entity } CREATE TABLE "order" (id INT NOT NULL, del...

Doctrine Cascade Operations

Cascading operations mean that when you perform a persist , remove , merge (deleted), detach , refresh (does not work) operation on an entity, the same operation will also be performed on the associated entity. Persist The persist operation saves the new entity to the database. The persist operation only needs to be performed when a new entity needs to be created, if the entity was obtained from the repository, then persist is not necessary, since the changes are already tracked by the EntityManager, it is enough to just flush . When specifying cascade: ['persist'] , persist will also be executed on the related entity. When cascade: ['persist'] is NOT specified #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product { #[ORM\OneToOne(mappedBy: 'product')] private ?Photo $photo = null; } #[ORM\Entity(repositoryClass: PhotoRepository::class)] class Photo { #[ORM\OneToOne(inversedBy: 'photo')] #[ORM\JoinColumn(nul...