kmaci
kmaci

Reputation: 3274

Unique values for two columns in Doctrine

I have entity called Dimension. It has three attributes - ID, width and height. ID is primary key. In the table, the dimension should be unique so there has to be only one record with given dimension (for example 40x30). What constraints I need to set? Is uniqueConstraints={@UniqueConstraint(name="dimension", columns={"width", "height"})} correct?

Upvotes: 3

Views: 7647

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

From the documentation,

@UniqueConstraint annotation is used inside the @Table annotation on the entity-class level. It allows to hint the SchemaTool to generate a database unique constraint on the specified table columns. It only has meaning in the SchemaTool schema generation context.

Required attributes:

  • name: Name of the Index
  • columns: Array of columns.

The anwser is then YES

/**
 * @Entity
 * @Table(name="xxx",uniqueConstraints={@UniqueConstraint(name="dimension", columns={"width", "height"})})
 */
class Dimension

should then do the job.

Upvotes: 14

Related Questions