Skip to content
Advertisement

Doctrine query using findBy and update entity

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

Advertisement

Answer

Use the flush() at the end of the process, it executes the query.

Meanwhile use the 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();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement