主义:具有多对多领域的独特性 [英] Doctrine: UniqueEntity with Many-to-Many field

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

问题描述

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



但是,Symfony抛出一个异常:


执行'SELECT t0.id AS id_1,t0.name
AS name_2,t0.minutes AS minutes_3,t0.safetytime AS securitytime_4,
t0.cost AS cost_5,t0.device_id AS device_id_6 FROM repair t0 WHERE
t0.name =? AND t0.device_id =? AND repairs_colors.color_id =?'with
params [Replace screen,2,{}]:



可追踪的致命错误 :class
的对象Doctrine\ORM\PersistentCollection无法转换为字符串


这是我的实体定义(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查询中传递。
));

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



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:

An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2, t0.minutes AS minutes_3, t0.safetytime AS safetytime_4, t0.cost AS cost_5, t0.device_id AS device_id_6 FROM repair t0 WHERE t0.name = ? AND t0.device_id = ? AND repairs_colors.color_id = ?' with params ["Replace screen", 2, {}]:

Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string

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 {
    // ...
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆