Zoeken…


Een Symfony Entity als YAML verklaren

  • 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: { }
  • Maak de tafel
php bin/console doctrine:schema:update --force

Of gebruik de aanbevolen manier (ervan uitgaande dat u al doctrine-migraties-bundel in uw project hebt geïnstalleerd):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
  • Creëer de entiteit automatisch vanuit de YAML

    php bin / console doctrine: genereren: entiteiten AppBundle

Een Symfony Entity declareren met annotaties

  • 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;
    }
}
  • Genereer de entiteit
php bin/console doctrine:generate:entities AppBundle/Person
  • Om de SQL-instructies naar het scherm te dumpen
php bin/console doctrine:schema:update --dump-sql
  • Maak de tafel
php bin/console doctrine:schema:update --force

Of gebruik de aanbevolen manier (ervan uitgaande dat u al doctrine-migraties-bundel in uw project hebt geïnstalleerd):

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


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow