0

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 :(

4
  • This question appears related: stackoverflow.com/questions/12001753/… Commented Apr 1, 2014 at 12:21
  • Thank you! But I don't have validation groups and I don't want to create. Commented Apr 1, 2014 at 12:32
  • @UniqueEntity({"name", "restaurant"}) Commented Apr 1, 2014 at 12:34
  • 1
    what do you mean by but it's not working :( I'm stopped by the schema with? When does this happen? Please elaborate. Commented Jun 21, 2017 at 3:49

3 Answers 3

1

To create a unique constraint on more than one field you have to add them to the fields argument in @UniqueEntity().

For example:

    @UniqueEntity(fields={"name", "restaurant"}).

I also hope this issue has since been resolved https://github.com/symfony/symfony/issues/6727

Sign up to request clarification or add additional context in comments.

Comments

0

I think your problem is that you already have data in your table that does not comply with you new constraint. Try truncating the table and updating again, or manually editing the data to reflect the new constraint prior updating the schema.

Comments

0

In my own UniqueEntity I had a customer field and an option field that had to be a unique combination. The customer was set by the request to the controller, not by the form itself, so the assert wasn't working. I was able to fix the problem by having the form Render the customer, then hiding that option in the rendered form view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.