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.