« Anterior - Versión 5/6 (diferencias) - Siguiente » - Versión actual
Guillermo Zdanowicz, 02/10/2015 21:41


Armado de Esquemas YML

A diferencia de Symfony 1, el esquema es armado por cada clase

Primero creamos la carepta doctrine

mkdir src/JobeetBundle/Resources/config/doctrine

Creamos y editamos los archivos de esquemas

mcedit src/JobeetBundle/Resources/config/doctrine/Category.orm.yml

# src/JobeetBundle/Resources/config/doctrine/Category.orm.yml
JobeetBundle\Entity\Category:
    type: entity
    table: category
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
            unique: true
    oneToMany:
        jobs:
            targetEntity: Job
            mappedBy: category
    manyToMany:
        affiliates:
            targetEntity: Affiliate
            mappedBy: categories

mcedit src/JobeetBundle/Resources/config/doctrine/Job.orm.yml

# src/JobeetBundle/Resources/config/doctrine/Job.orm.yml
JobeetBundle\Entity\Job:
    type: entity
    table: job
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        type:
            type: string
            length: 255
            nullable: true
        company:
            type: string
            length: 255
        logo:
            type: string
            length: 255
            nullable: true
        url:
            type: string
            length: 255
            nullable: true
        position:
            type: string
            length: 255
        location:
            type: string
            length: 255
        description:
            type: text
        how_to_apply:
            type: text
        token:
            type: string
            length: 255
            unique: true
        is_public:
            type: boolean
            nullable: true
        is_activated:
            type: boolean
            nullable: true
        email:
            type: string
            length: 255
        expires_at:
            type: datetime
        created_at:
            type: datetime
        updated_at:
            type: datetime
            nullable: true
    manyToOne:
        category:
            targetEntity: Category
            inversedBy: jobs
            joinColumn:
                name: category_id
                referencedColumnName: id
    lifecycleCallbacks:
        prePersist: [ setCreatedAtValue ]
        preUpdate: [ setUpdatedAtValue ]

mcedit src/JobeetBundle/Resources/config/doctrine/Affiliate.orm.yml

# src/JobeetBundle/Resources/config/doctrine/Affiliate.orm.yml
JobeetBundle\Entity\Affiliate:
    type: entity
    table: affiliate
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        url:
            type: string
            length: 255
        email:
            type: string
            length: 255
            unique: true
        token:
            type: string
            length: 255
        is_active:
            type: boolean
            nullable: true
        created_at:
            type: datetime
    manyToMany:
        categories:
            targetEntity: Category
            joinTable:
                name: category_affiliate
                joinColumns:
                    affiliate_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    category_id:
                        referencedColumnName: id
    lifecycleCallbacks:
        prePersist: [ setCreatedAtValue ]

Generamos los archivos de Entidades

php app/console doctrine:generate:entities JobeetBundle

Si no hemos creado la base de datos y tenemos configuraod en app/config/parameters.yml el acceso, podemos crearla

ejemplo de parameters.yml

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: s27
    database_user: root
    database_password: MICLAVE_MYSQL
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: dedeb278ce6f7db6ea0c78fa04499b87e418f613

php app/console doctrine:database:create

Luego trasladamos a la base de datos el esquema, en este caso conviene ya tener creada la base de datos segun lo configurado en parameters.yml

php app/console doctrine:schema:update --force

Referencias:

http://symfony.com/doc/current/book/doctrine.html

Redmine Appliance - Powered by TurnKey Linux