When the origin inverse side use for loop collection, the change set of owning side is reset. #6186

Open
opened 2026-01-22 15:28:29 +01:00 by admin · 2 comments
Owner

Originally created by @kevintoshi0208 on GitHub (Feb 23, 2019).

Bug Report

Q A
BC Break yes
Version 2.6.3

Summary

There is a One To Many RelationShip.
The owing side is Transaction, and the inverse side is Supplier.

When the origin inverse side collection for loop ,the owning side is reset.

     $em = $this->getDoctrine()->getManager();
        /** @var WsTransaction $transaction */

        $transaction = $this->getDoctrine()->getRepository(WsTransaction::class)
            ->find(1);
        $transaction->setSaleAmount(1000);

        $oriSupplier = $transaction->getSupplier();

        //output:  oriSupplier:5
        dump("oriSupplier:".$oriSupplier->getId());

        $supplier = $this->getDoctrine()->getRepository(WsSupplier::class)
            ->find(8);
        $transaction->setSupplier($supplier);

        $em->persist($transaction);

        //output: 8
        dump($transaction->getSupplier()->getId());

        $amount = 0;
        foreach ($oriSupplier->getWsTransactions() as $transaction){
            $amount = $transaction->getSaleAmount();
        }
        $oriSupplier->setOccupiedCredit($amount);

        //expects 8 but output is 5
        dump($transaction->getSupplier()->getId());
        $em->persist($transaction);

        $em->flush();

        //expects 8  but output is 5
        dump($transaction->getSupplier()->getId());

How can I do that ?

Originally created by @kevintoshi0208 on GitHub (Feb 23, 2019). ### Bug Report | Q | A |------------ | ------ | BC Break | yes | Version | 2.6.3 #### Summary There is a One To Many RelationShip. The owing side is Transaction, and the inverse side is Supplier. When the origin inverse side collection for loop ,the owning side is reset. ```php $em = $this->getDoctrine()->getManager(); /** @var WsTransaction $transaction */ $transaction = $this->getDoctrine()->getRepository(WsTransaction::class) ->find(1); $transaction->setSaleAmount(1000); $oriSupplier = $transaction->getSupplier(); //output: oriSupplier:5 dump("oriSupplier:".$oriSupplier->getId()); $supplier = $this->getDoctrine()->getRepository(WsSupplier::class) ->find(8); $transaction->setSupplier($supplier); $em->persist($transaction); //output: 8 dump($transaction->getSupplier()->getId()); $amount = 0; foreach ($oriSupplier->getWsTransactions() as $transaction){ $amount = $transaction->getSaleAmount(); } $oriSupplier->setOccupiedCredit($amount); //expects 8 but output is 5 dump($transaction->getSupplier()->getId()); $em->persist($transaction); $em->flush(); //expects 8 but output is 5 dump($transaction->getSupplier()->getId()); ``` How can I do that ?
Author
Owner

@Ocramius commented on GitHub (Feb 23, 2019):

Could you reduce the code example a bit? I'm not sure if I understand the issue. Is the association being overwritten by lazy loading?

@Ocramius commented on GitHub (Feb 23, 2019): Could you reduce the code example a bit? I'm not sure if I understand the issue. Is the association being overwritten by lazy loading?
Author
Owner

@kevintoshi0208 commented on GitHub (Feb 25, 2019):

Thank you for your keyword. I solved it by setting the fetch options of both side to EAGER .

@kevintoshi0208 commented on GitHub (Feb 25, 2019): Thank you for your keyword. I solved it by setting the fetch options of both side to EAGER .
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6186