Search code examples
doctrine-ormentity-relationshipentitiesunique-index

Doctrine OneToOne relationship - unique constraints


I have this scenario:

enter image description here



The entity MealListDay is one day, which has six meals (entity Meal). Now I use OneToOne relationship. But there is problem, because more days can not have same entity Meal - error: unique constraints. I know, that the entity Meal must be unique in OneToOne relationship, but is there any solution with using only these two tables?

Any idea? Thanks.

Entity MealListDay

    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue
    * @var integer
    */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */
    protected $elevenses;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */
    protected $soup;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */
    protected $mainMeal;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */
    protected $sideDish;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */
    protected $drink;

    /**
     * @ORM\OneToOne(targetEntity="Meal", cascade={"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @var Meal
     */

   protected $nosh;

Entity meal:

/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var integer
*/
private $id;

/**
 * @ORM\Column(type="string", length=150)
 */
protected $name;

/**
 * @ORM\Column(type="string", length=20)
 */
protected $type;

/**
 * @ORM\Column(type="simple_array", nullable=true)
 */
protected $allergens;

Solution

  • I must implement relation M:N, there is result. I know, that property allergens is not in 1NF, but these are only numbers, which links to specific allergen name and description which are defined statically in class.

    enter image description here