问题描述
实体
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ORMTable(name="_apiKey")
* @ORMEntity(repositoryClass="EveProfileBundleEntityRepositoryapiKeyRepository")
* @UniqueEntity(fields={"keyID", "vCode", "accountID"},
* message="you already own this api")
*/
class apiKey
{
public function __construct()
{
// empty
}
// relations start
// relations end
/**
* @ORMColumn(name="entryID", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $entryID;
/**
* @ORMColumn(name="accountID", type="integer", nullable=false, unique=true)
*/
private $accountID;
/**
* @ORMColumn(name="keyID", type="integer", nullable=false, unique=true)
* @AssertNotBlank(message="keyID cannot be blank")
*/
private $keyID;
/**
* @ORMColumn(name="vCode", type="string", nullable=false, unique=true)
* @AssertNotBlank(message="vCode cannot be blank")
*/
private $vCode;
在数据库中
CREATE TABLE IF NOT EXISTS `_apiKey` (
`entryID` int(11) NOT NULL AUTO_INCREMENT,
`accountID` int(11) NOT NULL,
`keyID` int(11) NOT NULL,
`vCode` varchar(255) NOT NULL,
PRIMARY KEY (`entryID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
当我尝试添加重复时出现错误
When i try to add dublicate i have an error
An exception occurred while executing 'INSERT INTO _apiKey (accountID, keyID, vCode) VALUES (?, ?, ?)' with params {"1":38,"2":"1233","3":"123"}:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '38-1233-123' for key 'accountID'
这很好,但我该如何处理呢?消息选项"在 @UniqueEntity() 中不起作用我如何在表单错误中显示此错误(无异常)
and it's fine, but how can i handle it? "message option" is not working in @UniqueEntity() how can i show this error in form errors (without exception)
我想要什么(没有 entryID 的 db 的简单示例)
what i want (simple example for db without entryID)
123 123 123 - add
123 123 1234 - add
123 125 123 - add
123 123 1234 - form-error on inserting
123 123 123 - form-error on inserting
234 123 123 - add
234 123 123 - form-error on inserting
推荐答案
如果您想单独检查 accountId 的唯一性,请将其添加到您的类中:
If you want to check the uniqueness of accountId alone, add this to your class:
@UniqueEntity(fields={"accountID"}, message="This account is already taken")
这里是检查组合keyID"、vCode"、accountID"的唯一性以及单独accountID"的唯一性的完整代码:
Here the full code that check the uniqueness of the combinaison "keyID", "vCode", "accountID" and also the uniqueness of "accountID" alone:
<?php
// ...
/**
* @ORMTable(name="_apiKey")
* @ORMEntity(repositoryClass="EveProfileBundleEntityRepositoryapiKeyRepository")
* @UniqueEntity(fields={"keyID", "vCode", "accountID"}, message="you already own this api")
* @UniqueEntity(fields={"accountID"}, message="This account ID is already taken")
*/
class apiKey
{
我不知道您是否看到该错误仅影响字段 accountID,如果您不希望出现这种行为,只需从您的属性 $accountID 中删除unique=true"即可.
I don't know if you saw that the error only affect the field accountID, if you don't want this behavior just remove "unique=true" from your property $accountID.
如果你只想说cominaisonkeyID"、vCode"、accountID"在数据库中必须是唯一的,可以这样操作:
If you just want to say that the cominaison "keyID", "vCode", "accountID" must be unique in the database, proceed like that:
/**
* @ORMTable(name="_apiKey",
* uniqueConstraints = {
* @ORMUniqueConstraint(name="_api_key_key_id_v_code_account_id", columns={"keyID", "vCode", "accountID})
* }
* )
*
* ...
* etc, ...
*/
class apiKey
这篇关于Symfony2,Doctrine,@UniqueEntity 处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!