Reputation: 1687
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;
in 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 ;
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'
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)
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
Upvotes: 1
Views: 4707
Reputation: 1687
trouble was in form Type class, to work this unique-combination you have to add all values to fields you want to be unique, on my example will be
$builder
->add('accountID', 'hidden', array ('data' => $options['acountID']))
->add('keyID', 'text')
->add('vCode', 'text');
i added hidden field (accontID) and fill it with data and then i got proper error to form-errors, if you will not fill there will be null values (so i don't know why it will NOT check uniques)
that is solution for me but basicly @Sybio help meto understand a lot of so i will check his answer as answer ^_^
Upvotes: 0
Reputation: 8645
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:
<?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
{
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
Upvotes: 4