0

I seem to have done the set up correctly, but still this does not work, ie, it will not set the database table correctly. In fact it ignores completely the @UniqueEntity annotation.

I am setting up a GEDMO tree, where the title of the category should not be repeated for the same parent_id.

So, looking at the the @UniqueEntity documentation and also at some prior code I built, this should work:

/App/Entity/Category

namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 * @UniqueEntity(
 *  fields={"parent", "title"}, // or fields={"parent_id", "title"}
 *  errorPath="title", 
 *  message="This title is already in use for this parent.") 
 * @ORM\Table(name="categories")
*/

class Category
{

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

/**
 * @ORM\Column(type="string", length=190)
 */
private $title;
.....
/**
 * @Gedmo\TreeParent
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
 * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
 */
private $parent;

....
}

There's actually a similar question in here, without a solution.

BernardA
  • 1,391
  • 19
  • 48
  • What do you mean by "it will not set the database table correctly"? `UniqueEntity` is part of symfony validator, which run on your php application level, it does not change anything on your database. – Iwan Wijaya Jun 18 '18 at 15:53
  • I was under the impression that it would create a composite key on the table. How otherwise would it know that the value to be inputted would violate a constraint? It has to refer to the db. – BernardA Jun 18 '18 at 16:42
  • By doing a query based on the specified fields. The validator does interact with the db, but it doesn't change it. UniqueEntity is only triggered when using symfony validator component. – Iwan Wijaya Jun 18 '18 at 18:46

1 Answers1

0

If you want checking on the database level you will need to use Doctrine's UniqueConstraint annotation:

/*
 * @ORM\Table(name="categories", uniqueConstraints={
 *     @ORM\UniqueConstraint(name="uq_cat_parent_title", columns={"parent_id", "title"})
 * })
 */

EDIT: Modified my answer to include the name of the index. Doctrine documentation states that it is required.

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
  • Thanks, but doctrine:migrations:diff will simply not pickup those annotations. – BernardA Jun 18 '18 at 17:14
  • What do you mean they won't pick up those annotations? – Jason Roman Jun 18 '18 at 17:20
  • If the unique index already exists in your database there's probably nothing additional to generate. I added a `name` to my answer for the unique key. – Jason Roman Jun 18 '18 at 17:27
  • The unique composite index does not already exists on the db. When I say migrations will not pick it up, it means that this is the output: No changes detected in your mapping information. – BernardA Jun 18 '18 at 17:32
  • Try adding `name` like I did to my answer. – Jason Roman Jun 18 '18 at 17:37
  • I had an issue with cache that was preventing migrations to pick up the change. So now it will generate the table index correctly BUT it will throw a 500 error when I attempt to add a value that duplicates the index. I'm looking to have a regular error sent back, as ASSERT does. – BernardA Jun 20 '18 at 06:51
  • You should get a Doctrine exception that you can try/catch. If you want it through the assert you'll have to use Symfony's Validator component. – Jason Roman Jun 20 '18 at 07:00