Symfony

Creating a Database With Symfony and Doctrine

Today, I created my first sample database using Symfony. In just a few steps I was able to have my db up and running. It was awesome!

First, I updated my db parameters in “parameters.yaml”. I was worried that this wouldn’t work because I’m using a Vagrant box (Laravel Homestead) and you don’t really see examples for this approach in the documentation. So, I just added theconfig items (db host, port, name, user, etc.) the same way I have them configured in Sequel Pro. It worked.

Here’s a sample parameters.yml:

parameters:
    database_host: 127.0.0.1
    database_port: 33060
    database_name: product_db
    database_user: homestead
    database_password: secret
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: dcc9090991abc12aaaadbb787111cee

Next, I created an Entity class.

<?php

namespace ProductApp\ManagerBundle\Entity;

class Product {
    protected $id;
    protected $title;
    protected $description;
}

The entity class contains the database table column names. You have different options for entering the data type or description here. You can add it using annotations, YAML or XML. I settled for YAML. Why? I thought it made sense since other configs are done the same way. I’m not a fan of annotations. If you’re wondering what I’m talking about, these are configs done on DocBlock.

Finally, the last step before running a series of really fun commands on your terminal is to create the Doctrine YAML file. This file contains the table column descriptions.

ProductApp\ManagerBundle\Entity\Product:
  type: entity
  table: product
  id:
    id:
      type: integer
      generator: { strategy: AUTO }
  fields:
    title:
      type: string
      length: 80
    description:
      type: text
      length: 300

I am used to creating my databases using MySQL syntax using a terminal or a client. So, this step felt a little awkward because the syntax (or I should say “mapping”) is a little bit different. You will need to get familiar with Doctrine, something I’ll be doing in the next few weeks before I jump into building a real app.

The cool thing is what comes now. Now, you open your terminal, run a few commands, and almost magically your db will be created.

$ php app/console doctrine:generate:entities ProductApp/ManagerBundle/Entity/Product
Generating entity "ProductApp\ManagerBundle\Entity\Product"
  > backing up Product.php to Product.php~
  > generating ProductApp\ManagerBundle\Entity\Product

$ php app/console doctrine:database:create
Created database `product_db` for connection named default

$ php app/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "1" queries were executed

I was curious to see this in MySQL. This is what I found:

mysql> describe product;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(150) | NO   |     | NULL    |                |
| description | text         | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> show table status\G
*************************** 1. row ***************************
           Name: product
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: 1
    Create_time: 2015-08-22 04:51:35
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_unicode_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

Reference:

  1. Databases and Doctrine
  2. Doctrine, Basic Mapping

 

Standard

Leave a Reply

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