2

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

4

2 に答える 2

0

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();
于 2012-04-24T18:55:09.190 に答える
-1

The solution is as follows:

$crack = $em->getRepository('CrackBundle:Crack')->findBy(array('id' => 1 ));
 foreach ($crack as $c) {
 $c->setName('Nilton');
 $em->persist($c);
 $em->flush();
 }   
于 2019-05-28T22:43:55.640 に答える