Symfony创建实体缺少唯一


symfony creating entities missing unique

Symfony没有将电子邮件字段标记为唯一。我可以在验证中设置它。是的,但不是一个好方法。有人知道symfony为什么不把邮箱字段标记为唯一吗?

用于从现有数据库创建实体的命令:

php app/console doctrine:mapping:convert xml ./src/Frontend/AccountBundle/Resources/config/doctrine/metadata/orm --from-database --force
php app/console doctrine:mapping:import FrontendAccountBundle annotation
php app/console doctrine:generate:entities FrontendAccountBundle

我的表:

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=22 ;
在config/…/User.orm.xml:

<field name="email" type="string" column="email" length="255" nullable="false"/>
在实体/User.php

/**
 * @var string
 *
 * @ORM'Column(name="email", type="string", length=255, nullable=false)
 */
private $email;

您需要指定该列必须包含像这样的惟一值:

@ORM'Column(name="email", type="string", unique=true, length=255, nullable=false)
                                         ^^^^^^^^^^^

@ORM'Column(name="username", type="string", length=25, unique=true)