dongpixi2648 2012-04-24 12:36
浏览 84
已采纳

使用findBy和更新实体的Doctrine查询

I am fairly new to Doctrine2 and I am trying to learn how to query entities and updating them.

The method I am using to query is the findBy method on a specific attribute to search for a list of records in the database, "my query returns a list of objects". Now I want to update a few of the attributes in the entity which I can not get working. Here is what I have:

  /** 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);

It gets to the first var dump in the loop which returns a list of objects but does not get to the second var dump because of using flush. if I perform without the flush method the attributes show the updated info in var_dump but just will not actually commit the updates since flush is not working. What am I doing wrong.

Also, the id's for the entity is id, and itemCnt

  • 写回答

2条回答 默认 最新

  • duanpo7282 2012-04-24 18:55
    关注

    use the flush() at the end of the process

    meanwhile use teh persist()

    ex:

        $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条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀