サーチ…


symfonyエンティティを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: { }
  • テーブルを作成する
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:生成:エンティティAppBundle

AnnotationでSymfonyエンティティを宣言する

  • 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;
    }
}
  • エンティティを生成する
php bin/console doctrine:generate:entities AppBundle/Person
  • SQL文を画面にダンプするには
php bin/console doctrine:schema:update --dump-sql
  • テーブルを作成する
php bin/console doctrine:schema:update --force

または、あなたのプロジェクトにdoctrine-migrations-bundleがすでにインストールされていることを前提として、推奨される方法を使用してください:

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


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow