使用註釋宣告 Symfony 實體
- appbundle /實體/ 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