Symfony2, Doctrine, @UniqueEntity handle exception

后端 未结 2 2008
时光说笑
时光说笑 2021-01-23 23:40

ENTITY

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


        
2条回答
  •  旧巷少年郎
    2021-01-24 00:21

    Duplicate entry '38-1233-123' for key '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")
    

    Here the full code that check the uniqueness of the combinaison "keyID", "vCode", "accountID" and also the uniqueness of "accountID" alone:

    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.

    If you just want to say that the cominaison "keyID", "vCode", "accountID" must be unique in the database, proceed like that:

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

提交回复
热议问题