How to update row with unique column in Doctrine

Viewed 53

I have a row with sku column that must be unique. But when I try to update this row it doesn't happen because of this. How to avoid this with Doctrine ? Or I should write custom logic ?

if (!$this->uniqueSkuSpecification->isSatisfiedBy($request->getSku())) {
            return ErrorResponse::fromCustomError(
                'Product sku must be unique',
                'sku',
                Response::HTTP_UNPROCESSABLE_ENTITY
            );
        }

The way I am populating the data from $request (its DTO) public function updateFields(UpdateProductCommand $request): Product { $reflection = new \ReflectionClass($this);

    foreach ($reflection->getProperties() as $property) {
        $field = ucfirst($property->getName());
        $getter = "get{$field}";
        $setter = "set{$field}";
        if (method_exists($this, $setter) && !empty($request->$getter())) {
            $this->$setter($request->$getter());
        }
    }

    return $this;
}

EDIT: I am saving/updating this way:

$this->entityManager->persist($product);
$this->entityManager->flush();
0 Answers
Related