我是Doctrine2的新手,我正在尝试学习如何查询实体并更新它们。
我用来查询的方法是在特定属性上的findBy方法,以搜索数据库中的记录列表,“我的查询返回一个对象列表”。现在我想更新实体中我无法工作的一些属性。这就是我所拥有的:
/** Set the search attributes for hls**/
$id = array("itemNbr" => $itemNbr);
$hls = $this->emInstance->getRepository('entity\\Hls')->findBy($id);
// on update hls
foreach($hls as $h){
$h->setAllRd($Rd);
$h->setRdy($Rdy);
$h->setNo($no);
var_dump($h);
}
$this->emInstance->flush();
var_dump($statHdr);
它到达循环中的第一个var转储,返回一个对象列表 但由于使用了flush,因此无法进入第二个var转储。如果我在没有flush方法的情况下执行,则属性会在var_dump中显示更新的信息,但实际上不会提交更新,因为flush不起作用。我做错了什么。
此外,实体的ID是id,itemCnt
答案 0 :(得分:0)
在流程结束时使用flush()
同时使用persist()
X:
$id = array("itemNbr" => $itemNbr);
$hls = $this->emInstance->getRepository('entity\\Hls')->findBy($id);
// on update hls
foreach($hls as $h){
$h->setAllRd($Rd);
$h->setRdy($Rdy);
$h->setNo($no);
var_dump($h);
//keep the changes in memory
$this->emInstance->persist($h);
}
var_dump($statHdr);
// insert the changes to the db
$this->emInstance->flush();
答案 1 :(得分:0)
解决方案如下:
$crack = $em->getRepository('CrackBundle:Crack')->findBy(array('id' => 1 ));
foreach ($crack as $c) {
$c->setName('Nilton');
$em->persist($c);
$em->flush();
}