Symfony2,Doctrine,@ UniqueEntity处理异常

时间:2013-01-27 11:44:20

标签: symfony exception-handling doctrine

ENTITY

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Table(name="_apiKey")
 * @ORM\Entity(repositoryClass="Eve\ProfileBundle\Entity\Repository\apiKeyRepository")
 * @UniqueEntity(fields={"keyID", "vCode", "accountID"}, 
 * message="you already own this api")
 */
class apiKey
{

public function __construct()
{
    // empty
}

// relations start
// relations end

/**
 * @ORM\Column(name="entryID", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $entryID;

/**
 * @ORM\Column(name="accountID", type="integer", nullable=false, unique=true)
 */
private $accountID;

/**
 * @ORM\Column(name="keyID", type="integer", nullable=false, unique=true)
 * @Assert\NotBlank(message="keyID cannot be blank")
 */
private $keyID;

/**
 * @ORM\Column(name="vCode", type="string", nullable=false, unique=true)
 * @Assert\NotBlank(message="vCode cannot be blank")
 */
private $vCode;
db中的

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 ;

当我尝试添加dublicate时,我有一个错误

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()如何在表单错误中显示此错误(无例外)

我想要什么(没有entryID的db的简单示例)

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

2 个答案:

答案 0 :(得分:4)

  

键'accountID'重复输入'38 -1233-123'

如果您想单独检查accountId的唯一性,请将其添加到您的班级:

@UniqueEntity(fields={"accountID"}, message="This account is already taken")

这里是完整的代码,用于检查组合“keyID”,“vCode”,“accountID”的唯一性以及“accountID”的唯一性:

<?php

// ...

/**
 * @ORM\Table(name="_apiKey")
 * @ORM\Entity(repositoryClass="Eve\ProfileBundle\Entity\Repository\apiKeyRepository")
 * @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”。

如果您只想说cominaison“keyID”,“vCode”,“accountID”在数据库中必须是唯一的,请按以下步骤操作:

/**
 * @ORM\Table(name="_apiKey",
 *      uniqueConstraints = {
 *          @ORM\UniqueConstraint(name="_api_key_key_id_v_code_account_id", columns={"keyID", "vCode", "accountID})
 *      }
 * )
 *
 * ...
 * etc, ...
 */
class apiKey

答案 1 :(得分:0)

麻烦在于表单类型,要处理这个独特的组合,你必须将所有值添加到你想要独特的字段,在我的例子中将是

  $builder
            ->add('accountID', 'hidden', array ('data' => $options['acountID']))
            ->add('keyID', 'text')
            ->add('vCode', 'text');

我添加了隐藏字段(accontID)并用数据填充它然后我得到了错误的表单错误,如果你不填充那里会有空值(所以我不知道为什么它不会检查唯一身份)

这对我来说是解决方案,但基本上@Sybio帮助我了解了很多,所以我会将他的答案检查为答案^ _ ^