Creating a Symfony project from zero to success in 52 weeks (week 2)
Miss the week 1?
Install and configure Doctrine ORM. You need it to persist data to a database. Although you don’t need an ORM, is insane not using one.
$ symfony composer require orm
You need to configure your database connection string. Create (if not exists) a .env.local file in the project root. Add the follow 3 lines (based on the stack and setup defined in the week 1 chapter):
###> doctrine/doctrine-bundle ###
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=mariadb-10.5.15&charset=utf8mb4"
###< doctrine/doctrine-bundle ###
Now create the database:
$ symfony console doctrine:database:create
Install the Symfony Security component.
$ symfony composer require security
Install Symfony Maker Bundle only for the “dev” environment. This will help you to create and scaffold your entire project, saving hours of work.
$ symfony composer require --dev maker
Create the initial security entity (User). We’ll store users and their (encrypted)passwords in our own database through the ORM and Symfony Security component. There are many other configurations on how you can store and manage users and security, but the previous is the one used in this project.
$ symfony console make:user# accept the default valuesThe name of the security user class (e.g. User) [User]Do you want to store user data in the database (via Doctrine)? (yes/no) [yes]Enter a property name that will be the unique "display" name for the user (e.g. email, username, uuid) [email]Does this app need to hash/check user passwords? (yes/no) [yes]
Check your new User entity in src/Entity/User.php and your new repository class in src/Repository/UserRepository.php, also check the providers section of the security.yaml file (config/packages/security.yaml). This is the configuration to manage users through ORM (MakerBundle do this for you):
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
Create the initial database schema. We won’t use migrations until the model is in a stable stage.
$ symfony console doctrine:schema:create
Upgrade subsequent data model changes (Doctrine entities) with:
$ symfony console doctrine:schema:update --force
And validate your model against the database with:
$ symfony console doctrine:schema:validate
See you next saturday.