G
G
guyasyou2017-05-29 15:35:33
MySQL
guyasyou, 2017-05-29 15:35:33

How to change order of saving OneToMany data in Doctrine (UPDATE before INSERT)?

There is a product, and it has combinations related to OneToMany. The combinations have JSON options
{"205":300,"206":307,"207":0}
and it is unique, and in the db the column is marked as a unique key.
The problem occurs when saving the product. Doctrine inserts new records first, and expectedly gets an error

1062 Duplicate entry '{"205":300,"206":307,"207":0}

Here is the code for adding new combinations.
/**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="ProductCombination", mappedBy="product", cascade={"persist", "refresh"})
     */
    protected $productCombinations;

public function updateProductCombinations($newCombinations) {

        foreach ($this->productCombinations as $currentCombination) {
            //Обнуляем опции и деактивируем
            $currentCombination->setOptionsJSON(NULL);
            $currentCombination->unapprove();
        }

        foreach ($newCombinations as $newCombination) {
                $price = null;
                $this->productCombinations->add($newCombination);
        }
    }

How can I force Doctrin to first update the old records and zero out the unique keys, and only then insert the new ones?
Or will you have to manually save the current records before saving the item?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question