我似乎已经正确完成了设置,但这仍然不起作用,即它不会正确设置数据库表。事实上,它完全忽略了 @UniqueEntity 注释。
我正在设置GEDMO 树,其中不应为相同的 parent_id 重复类别的标题。
因此,查看@UniqueEntity 文档以及我构建的一些先前代码,这应该可以工作:
/应用程序/实体/类别
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;
....
}
这里实际上有一个类似的问题,没有解决方案。