將 Symfony 實體宣告為 YAML

  • appbundle /實體/ 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 /資源/配置/教義/ 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: { }
  • 建立表
php bin/console doctrine:schema:update --force

或者使用推薦的方式(假設你已在專案中安裝了 doctrine-migrations-bundle):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
  • 從 YAML 自動建立實體

    php bin / console doctrine:generate:entities AppBundle