1

I have been searching on google for this and am not able to find the answer. I'm new to Symfony. In some places I hear about schema.yml and you can create all the tables with it. But in Symfony2, exactly where do we create that file? The following page: http://symfony.com/doc/2.0/book/doctrine.html, really doesn't answer that question and its from the Symfony page..

Is there any documentation that can answer this question? For Symfony1.x I hear that you can create a schema.yml file and then run a create shema command to create all the tables etc. Right now i'm able to create the database, but how can I create the tables? Where do I define them in Symfony2?

1 Answer 1

2

In Symfony2, you need to define the mapping entity by entity. There is three different kind of mapping: Annotation, YAML and XML mapping.

The Symfony page you are linking does answer the question to this question. For example, if you got to the Mapping section, you will see this paragraph:

For Doctrine to be able to do this, you just have to create "metadata", or configuration that tells Doctrine exactly how the Product class and its properties should be mapped to the database. This metadata can be specified in a number of different formats including YAML, XML or directly inside the Product class via annotations:

Then, if you go just a bit below, you see a tabbed pane with the three mapping options. Click on the YAML tab and you will see:

# src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.yml
Acme\StoreBundle\Entity\Product:
    type: entity
    table: product
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        price:
            type: decimal
            scale: 2
        description:
            type: text

This piece of YAML is the mapping informations and is located at this location: src/Acme/StoreBundle/Resources/config/doctrine/EntityName.orm.yml. The EntityName part should be changed with the name of your own entity. If you entity class is Product, the it will be Product.orm.yml. You will need to define multiple .yml files to define mutliple entities.

The next step is to populate the database schema. Assuming your configuration is good, you will use these commands to respectively drop and create the database:

php console doctrine:database:drop --force
php console doctrine:database:create

Then, to populate the schema, you must use this command:

php console doctrine:schema:update --force

The documentation for Doctrine is the place to check for other mapping informations and all the stuff you need for database interaction in Symfony2 via Doctrine.

Regards,
Matt

2
  • Thanks! Exactly what I was looking for. I saw that page too, but for some reason it wasn't describing everything the way you described.
    – KVISH
    Mar 2, 2012 at 1:57
  • No problem, sometimes a different way to explain stuff can help. Happy coding :)
    – Matt
    Mar 2, 2012 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.