Doctrine @UniqueEntity with ManyToOne fields?

I am trying to create a UniqueEntity with 2 fields (both are ManyToOne fields).

The code is as follows:

/* * @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 make sure that I have only one entity that binds the USER to CONNECT.

Should I write something else in the @UniqueEntity ad?

+4
source share
3 answers

I understand that you want to receive an error message only when both user and connection fields for one record are duplicated in another record in the database.

The @UniqueEntity annotation is rightly declared for your purpose (multiple column index), but will only be activated in form validation and will not affect the DDBB schema.

If you want to add the same validation at the database level, you must use the @UniqueConstraint annotation in the Table () declaration and provide a name for the new index. Sort of:

 /* * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="IDX_USER_CONNECT", columns={"user_id", "connect_id"})}) * @ORM\Entity * @ORM\HasLifecycleCallbacks() * @UniqueEntity(fields={"user", "connect"}) */ class UserConnect { 

On the other hand, if you declare @ORM \ Column (unique = true) in each attribute, you will get a completely different behavior, it will not be an index with several columns, but you will have two independent unique columns, if you enter the same one twice same user_id, you will get an error regardless of the value of connect_id, and the same thing will happen if you enter twice the same value of connect_id.

+7
source

It works:

 /** * State * * @ORM\Table( * name="general.states", * uniqueConstraints={ * @ORM\UniqueConstraint(name="states_country_name_code_key", columns={"idcountry", "name","code"}), * }) * @ORM\Entity(repositoryClass="Fluency\Bundle\GeneralBundle\Entity\Repository\StateRepository") */ class State {....... 

Taken from an object in my system. This method affects the database schema. See where I put @\ORM\UniqueConstraint annotation . Sorry @estopero ... next time I have to read the other answers first.

+5
source

You must also add a unique declaration to your attribute annotations.

  /** * @ORM\ManyToOne(targetEntity="User", inversedBy="userConnects") * @ORM\Column(unique=true) */ private $user; /** * @ORM\ManyToOne(targetEntity="Connect", inversedBy="userConnects") * @ORM\Column(unique=true) */ private $connect; 

See symfony doc and this StackOverflow answer.

-1
source

All Articles