Symfony 7: Doctrine
Setup
composer require doctrine
doctrine/doctrine-bundle instructions:
* Modify your DATABASE_URL config in .env
* Configure the driver (postgresql) and
server_version (16) in config/packages/doctrine.yaml
Use MariaDB instead of PostgeSQL
- delete
compose.yml
andcompose.override.yml
composer require symfony/maker-bundle --dev
syc make:docker:database
The new "database" service is now ready!
Next:
A) Run docker-compose up -d database to start your database container
or docker-compose up -d to start all of them.
B) If you are using the Symfony Binary, it will detect the new service automatically.
Run symfony var:export --multiline to see the environment variables the binary is exposing.
These will override any values you have in your .env files.
C) Run docker-compose stop will stop all the containers in compose.yaml.
docker-compose down will stop and destroy the containers.
Port 3306 will be exposed to a random port on your host machine.
Add db name, user and password to compose.yaml
:
MYSQL_DATABASE: app
MYSQL_PASSWORD: app
MYSQL_USER: app
This helps Doctrine determine which features your database does or doesn't support:
config/packages/doctrine.yaml
server_version: '10.5'
Development with Docker
# start
docker-compose up -d
# info
docker-compose ps
# connect from local
mysql --user=app --port=32769 --host=127.0.0.1 --password app
# password: app
# connect from container
docker-compose exec database mysql --user=app --password app
Misc
# run a query
symfony console dbal:run-sql 'SELECT * FROM address' --env=test
symfony console doctrine:query:sql 'SELECT * FROM address'
Create database
- Make sure to use symfony command when using Docker;
symfony console doctrine:database:create
# drop
symfony console doctrine:database:drop --force
Entity
- only getters and setters: Anemic model
- custom methods: Rich model
# create or update
symfony console make:entity
Migrations
# show sql that would be executed
symfony console doctrine:schema:update --dump-sql
# create
symfony console make:migration
# run
symfony console doctrine:migrations:migrate
# status
symfony console doctrine:migrations:status
# list
symfony console doctrine:migrations:list
Persisting to the Database
EntityManagerInterface
- "persist(object)" (does not actually save the object or call the db);
- can be run repeatedly with different objects (using same object has no effect);
- "flush" (performs operation from persist);
Querying the Database
- use repository as parameter
- alternatively:
$repository = $entityManager->getRepository(Address::class);
- custom query: add method in repository;
Extensions
Timestampable
-
add created / updated fields
-
Install
composer require stof/doctrine-extensions-bundle
- Enable:
config/packages/stof_doctrine_extensions.yaml
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
timestampable: true
Entity: use TimestampableEntity;
- Create migration.
Sluggable
- https://symfonycasts.com/screencast/symfony-doctrine/sluggable
Data Fixtures
- dummy data
composer require --dev orm-fixtures
# clear db and execute load methods
symfony console doctrine:fixtures:load
Foundry
composer require zenstruck/foundry --dev
# create factory
symfony console make:factory
Pagination
composer require babdev/pagerfanta-bundle pagerfanta/doctrine-orm-adapter
composer require pagerfanta/twig
config/packages/babdev_pagerfanta.yaml
babdev_pagerfanta:
default_view: twig
default_twig_template: '@BabDevPagerfanta/tailwind.html.twig'
symfony console cache:clear