Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Database Config and Automatic Table Creation

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We described the genus table to Doctrine via annotations, but this table doesn't exist yet. No worries - Doctrine can create it for us!

And actually, we don't even have a database yet. Doctrine can also handle this. Head to the terminal use the console to run:

./bin/console doctrine:database:create

But wait! Can Doctrine do this yet? We haven't told it anything about the database: not the name we want, the user or the password.

Configuring the Database

Where do we do that? The same place that everything, meaning all services are configured: app/config/config.yml. Scroll down to the doctrine key:

... lines 1 - 43
# Doctrine Configuration
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
... lines 60 - 72

Ah, this is what tells Doctrine all about your database connection.

But, the information is not hardcoded here - these are references to parameters that are defined in parameters.yml:

... lines 1 - 3
database_port: ~
database_name: symfony
database_user: root
database_password: ~
# You should uncomment this if you want use pdo_sqlite
# database_path: "%kernel.root_dir%/data.db3"
... lines 12 - 15

Update the database_name to aqua_note and on my super-secure local machine, the database user is root with no password.

Go Deeper!

Find out more about these parameters in our Symfony Fundamentals Series.

Back to the terminal! Now hit enter on the command:

./bin/console doctrine:database:create

Database created. To create the table, run:

./bin/console doctrine:schema:update --dump-sql

This looks great - CREATE TABLE genus with the two columns. But this didn't execute the query yet - the --dump-sql option is used to preview the query if you're curious. Replace it with --force.

./bin/console doctrine:schema:update --force

So hey guys, this is really cool - we can be totally lazy and let Doctrine do all the heavy database-lifting for us. This doctrine:schema:update command is actually more powerful than it looks - it's going to "wow" us in a few minutes.

But first, let's learn how to insert data into the new table.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
        "doctrine/doctrine-migrations-bundle": "^1.1" // 1.1.1
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0", // v3.1.3
        "nelmio/alice": "^2.1", // 2.1.4
        "doctrine/doctrine-fixtures-bundle": "^2.3" // 2.3.0