I have restaurants and categories - each category has one restaurant and each restaurant can have many categories. I want for each restaurant the categories to have unique names and I want if someone tries to break this, to get message that the form is not valid, not to be stopped by the schema. So here is what I try:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="categories", uniqueConstraints={@ORM\UniqueConstraint(name="name_restaurant_id_idx", columns={"name", "restaurant_id"})}))
* @ORM\Entity(repositoryClass="MyBundle\Entity\CategoryRepository")
* @UniqueEntity({"name", "restaurant"})
*/
class Category
{
/** @ORM\Column(name="category_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* @ORM\Column(name="name", type="string", length=45, nullable=false, unique=true)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Restaurant")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="restaurant_id", referencedColumnName="restaurant_id", unique=true)
* })
*/
private $restaurant;
but it's not working :( I'm stopped by the schema with
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
I also tried * @UniqueEntity({"name", "restaurant_id"}), but I get an error that restaurant_id is not mapped by Doctrine.
Any ideas why it's not working :(
@UniqueEntity({"name", "restaurant"})but it's not working :( I'm stopped by the schema with? When does this happen? Please elaborate.