Answer the question
In order to leave comments, you need to log in
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}
/**
* @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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question