主义:具有多对多领域的独特性 [英] Doctrine: UniqueEntity with Many-to-Many field
问题描述
但是,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屋!