本文介绍了主义:具有多对多领域的独特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类修复,应该有name,device和color的唯一条目。即



但是,Symfony抛出一个异常:

这是我的实体定义(Repair.php):

  / ** 
* @ ORM\Table name =repair)
* @ ORM\Entity(repositoryClass =AppBundle\Repository\RepairRepository)
* @UniqueEntity(fields = {name,device,colors },message =此设备已经存在名称为{{value}}的维修工作和这些颜色。)
* /
类修复{

/ **
* @var string
* @ ORM\Column(name =name,type =string,length = 255)
* /
private $ name ;

/ **
* @ ORM\ManyToOne(targetEntity =Device,inversedBy =repair)
* @ ORM\JoinColumn(name =device_id ,referencedColumnName =id)
* /
private $ device;

/ **
* @ ORM\ManyToMany(targetEntity =Color,inversedBy =repair)
* @ ORM\JoinTable(name =repairs_colors ,
* joinColumns = {@ ORM\JoinColumn(name =repair_id,referencedColumnName =id)},
* inverseJoinColumns = {@ ORM\JoinColumn(name =color_id,referencedColumnName =id)}
*)
* /
private $ colors;
}

我明白Symfony正在传递一个空的数组颜色,而不是实际的颜色组。但是为什么?

解决方案

是Symfony中UniqueEntity约束的源代码。



作为默认的Symfony会使内部人员进行如下查询:

   - > findBy(array(
'name' => $ entity-> getName(),
'device'=> $ entity-> getName(),
'colors'=> $ entity-> getColors()/ /< - 正是在这里,教义不能在SQL查询中传递。
));

解决方法:创建您自己的,并在您的约束中使用它:



pre $ / **
* @ ORM\Table(name =repair)
* @ ORM\Entity(repositoryClass =AppBundle\Repository \RepairRepository)
* @UniqueEntity(fields = {name,device,colors},repositoryMethod =getSimilarRepairs)
* /
class Repair {
// ...
}


I have a class Repair that should have unique entries for "name", "device", and "color". I.e. there should only be one "Replace screen" for "iPhone 5c" in colours "red" and "green".

However, Symfony throws an exception:

Here is my Entity definition (Repair.php):

/**
 * @ORM\Table(name="repair")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository")
 * @UniqueEntity(fields={"name", "device", "colors"}, message="There already exists a repair job with name {{ value }} for this device and these colours.")
 */
class Repair {

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Device", inversedBy="repairs")
     * @ORM\JoinColumn(name="device_id", referencedColumnName="id")
     */
    private $device;

    /**
     * @ORM\ManyToMany(targetEntity="Color", inversedBy="repairs")
     * @ORM\JoinTable(name="repairs_colors",
     *      joinColumns={@ORM\JoinColumn(name="repair_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="color_id", referencedColumnName="id")}
     *      )
     */
    private $colors;
}

I understand that Symfony is passing an empty array of colours instead of the actual colours set. But why?

解决方案

Here is the source code of the UniqueEntity constraint in Symfony.

As default Symfony will internaly do a query like :

->findBy(array(
'name'=>$entity->getName(),
'device'=>$entity->getName(),
'colors'=>$entity->getColors() // <- Precisely here, doctrine is not able to traduce that in a SQL query.
));

Workaround : Create you own Repository method, and use it in your constraint :

/**
 * @ORM\Table(name="repair")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository")
 * @UniqueEntity(fields={"name", "device", "colors"}, repositoryMethod="getSimilarRepairs")
 */
class Repair {
    // ...
}

这篇关于主义:具有多对多领域的独特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:06