doubo7131 2018-06-18 15:32
浏览 80

Symfony 4 @UniqueEntity不适用于GEDMO树复合字段

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.

  • 写回答

1条回答 默认 最新

  • doubing3662 2018-06-18 16:49
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图