用php8注解的方式,添加doctrine/orm里面的组合唯一索引

如果要添加多个唯一组合索引的话 use Doctrine\ORM\Mapping\UniqueConstraint 取多个别名应该可以

namespace App\Entity;

use App\Repository\AccountRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\UniqueConstraint;

#[ORM\Entity(repositoryClass: AccountRepository::class)]
#[UniqueConstraint(name:"account_ux", fields:["type", "value"])]      // 这一行定义一个唯一索引实例的初始化参数                        
#[ORM\Table(uniqueConstraints: [UniqueConstraint::class])] // 这一行定义table需要添加的唯一索引
class Account
{
    const TYPE_ACCOUNT  = 0;
    const TYPE_EMAIL    = 1;
    
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'smallint')]
    private $type;

    #[ORM\Column(type: 'string', length: 255)]
    private $value;

    #[ORM\Column(type: 'string', length: 13, unique: true)]
    private $userId;

    #[ORM\Column(type: 'boolean')]
    private $enabled;

    #[ORM\Column(type: 'integer')]
    private $createTime;

    #[ORM\Column(type: 'string', length: 255)]
    private $createUser;

    #[ORM\Column(type: 'integer')]
    private $updateTime;

    #[ORM\Column(type: 'string', length: 255)]
    private $updateUser;

    #[ORM\Column(type: 'string', length: 255)]
    private $updateIp;
.... 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章