使用注释声明 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