I'm trying to create a UniqueEntity with 2 fields (both are ManyToOne fields).
The code is as follow:
/*
* @ORM\Table()
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"user", "connect"})
*/
class UserConnect
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var boolean $isLeader
*
* @ORM\Column(name="isLeader", type="boolean")
*/
private $isLeader;
/**
* @var date $joinedDate
*
* @ORM\Column(name="joinedDate", type="date")
*/
private $joinedDate;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="userConnects")
*
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="Connect", inversedBy="userConnects")
*
*/
private $connect;
The goal is to ensure that I've got only one Entity that link a USER with a CONNECT.
Should I write something else in my @UniqueEntity
declaration?
I understand you want to get an error only when both user and connect fields for one record are duplicated in other record in the database.
The @UniqueEntity annotation is rightly declared for your purpose (multiple column index) but only will be triggered in the form validation and doesn't affects the DDBB schema.
If you want to add the same check at database level you should use the @UniqueConstraint annotation in the Table() declaration and give a name to the new index. Something like:
/*
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="IDX_USER_CONNECT", columns={"user_id", "connect_id"})})
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"user", "connect"})
*/
class UserConnect
{
In the other hand, if you declare @ORM\Column(unique=true) in each attribute you will get a very different behavior, it won't be a multiple column index but you will have two independent unique columns, if you enter twice the same user_id you will get an error independently of the connect_id value, and the same will happens if you enter twice the same connect_id value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With