Buscar..


Declarar una entidad de Symfony como YAML

  • AppBundle / Entity / Person.php
<?php

namespace AppBundle\Entity;

/**
 * Person
 */
class Person
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var int
     */
    private $age;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Person
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set age
     *
     * @param integer $age
     *
     * @return Person
     */
    public function setAge($age)
    {
        $this->age = $age;

        return $this;
    }

    /**
     * Get age
     *
     * @return int
     */
    public function getAge()
    {
        return $this->age;
    }
}
  • AppBundle / Resources / config / doctrine / Person.orm.yml
AppBundle\Entity\Person:
    type: entity
    repositoryClass: AppBundle\Repository\PersonRepository
    table: persons
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 20
            nullable: false
            column: Name
            unique: true
        age:
            type: integer
            nullable: false
            column: Age
            unique: false
    lifecycleCallbacks: { }
  • Crear la tabla
php bin/console doctrine:schema:update --force

O use la forma recomendada (asumiendo que ya tiene el paquete doctrina-migrations instalado en su proyecto):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
  • Crea la entidad automáticamente desde el YAML.

    doctrina php bin / console: generar: entidades AppBundle

Declarando una Entidad Symfony con anotaciones

  • AppBundle / Entity / Person.php
<?php

namespace AppBundle\Entity;

use DoctrineORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="persons")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PersonRepository")
 * @UniqueEntity("name")
 */
class Person
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", name="name", length=20, nullable=false)
     */
    protected $name;

    /**
     * @ORM\Column(type="integer", name="age", nullable=false)
     */
    protected $age;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getAge()
    {
        return $this->age;
    }

    public function setAge($age)
    {
        $this->age = $age;
        return $this;
    }
}
  • Generar la entidad
php bin/console doctrine:generate:entities AppBundle/Person
  • Para volcar las sentencias de SQL a la pantalla.
php bin/console doctrine:schema:update --dump-sql
  • Crear la tabla
php bin/console doctrine:schema:update --force

O use la forma recomendada (asumiendo que ya tiene el paquete doctrina-migrations instalado en su proyecto):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow