我有一个类修复,应该有" name"," device"和" color"的唯一条目。即应该只有一个"替换屏幕"用于" iPhone 5c"在颜色"红色"和"绿色"。
然而,Symfony引发了一个例外:
执行' 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 =?'同 参数["替换屏幕",2,{}]:
捕获致命错误:类的对象 Doctrine \ ORM \ PersistentCollection无法转换为字符串
这是我的实体定义(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;
}
据我所知,Symfony传递的是一组空的颜色,而不是实际的颜色集。但为什么呢?
答案 0 :(得分:1)
Here是Symfony中UniqueEntity约束的源代码。
默认情况下,Symfony将会执行以下查询:
->findBy(array(
'name'=>$entity->getName(),
'device'=>$entity->getName(),
'colors'=>$entity->getColors() // <- Precisely here, doctrine is not able to traduce that in a SQL query.
));
解决方法:创建您自己的Repository method,并在约束中使用它:
/**
* @ORM\Table(name="repair")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository")
* @UniqueEntity(fields={"name", "device", "colors"}, repositoryMethod="getSimilarRepairs")
*/
class Repair {
// ...
}