在symfony命令脚本中,我发现了这样的实体:
$this->em->getRepository('AppBundle:House')->findBy(...);
Foreach元素,我以我的元素作为参数调用服务方法。 在我的服务中,尝试创建链接到“房屋”对象的元素时出现错误
这不起作用:
$glass = $this->em->getRepository('AppBundle:Glass')->findOneBy(['house' => $house->getId(), 'name' => $name]);
if (! $glass instanceof Glass) {
$newGlass= new Glass($house, $name);
$this->em->persist($newGlass);
$this->em->flush();
}
这有效,但是我不想再次找到我已经在命令脚本中建立的现有房屋:
$existingHouse = $this->em->getRepository('House')->find($house->getId());
$glass = $this->em->getRepository('AppBundle:Glass')->findOneBy(['house' => $house->getId(), 'name' => $name]);
if (! $glass instanceof Glass) {
$newGlass= new Glass($existingHouse, $name);
$this->em->persist($newGlass);
$this->em->flush();
}
您能帮我弄清楚怎么了吗?
玻璃构造器
public function __construct(House $house, string $name)
{
$this->name= $name;
$house->addGlass($this);
$this->house= $house;
}
内部orm文件:
oneToMany:
glasses:
targetEntity: Glass
mappedBy: house
cascade: ["persist"]
玻璃orm文件:
manyToOne:
house:
targetEntity: House
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: glasses
joinColumns:
house_id:
referencedColumnName: id
orphanRemoval: false