That's my first question here on Stackoverflow.com and before I'll write to much. First the controller function:
/**
* @Rest\Patch("/identifiers/v2/{id}")
*
* @ParamConverter("identifier")
* @ParamConverter("identifierPatch", converter="fos_rest.request_body")
*/
public function patchAction(Identifier $identifier, Identifier $identifierPatch)
{
$identifier->setLandingPage($identifierPatch->getLandingPage());
$identifier->setIdentifier($identifierPatch->getIdentifier());
$identifier->setIsUsed($identifierPatch->getIsUsed());
$this->entityManager->flush();
/**
* Just for debugging...
*/
$view = $this->view([
'identifier' => $identifier,
'identifierPatch' => $identifierPatch
]);
return $this->handleView($view);
}
When i try to UPDATE an existing entity this way I get an ORMInvalidArgumentException with a message "A new entity was found through the relationship (...)"
When I set cascade={"persist"}
on the related entity:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\LandingPage", inversedBy="identifiers")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotNull()
* @Serializer\Type("App\Entity\LandingPage")
*/
private $landing_page;
... the related entity will be inserted as new entity and that's not what I am looking for.
I could use $this->entityManager->merge($identifier)
but that's not what I am looking for aswell, because I'll need to do some manual validations in future and I would like to return the entity as response (the related entity will be null when not updated) and $this->entityManager->merge()
will be deprecated in Doctrine 3.
Question: Is there any way to update the given entity with the deserialized entity?
Greetings, Danny Endert
EDIT (1): Well, I guess i found a solution regarding this "issue".
services.yaml
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Now I'm not getting any exception and the related entity will not be inserted as new entity.