I have a method within my controller that takes a deserialized object from JMSSerializerBundle and returns the full object, ready to be persisted. The reason I’m doing this is to return the updated object so, when it’s persisted, it doesn’t reset everything to the default.
When ran, this method ends up throwing an ErrorException on the indicated line and puts this in my log: Warning: ReflectionProperty::setValue() expects parameter 1 to be object, null given in C:\DevelopmentProjects\keobi-web\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadata.php line 177 (uncaught exception) at C:\DevelopmentProjects\keobi-web\vendor\symfony\src\Symfony\Component\HttpKernel\Debug\ErrorHandler.php line 65
For some reason, the EntityManager method find is returning (according to get_class) DefaultController, the controller class for this method. And the setFieldValue says it’s NULL.
One thing that’s confusing me is the Doctrine\ORM\EntityNotFoundException should be thrown if the entity isn’t found. It’s not being thrown. So, it’s obviously finding the entity, not not returning it properly…
Incoming parameter $data is the object deserialized by JSMSerializerBundle. $em is definitely EntityManager and $metadata is definitely ClassMetadata.
I’m at a loss here. No idea how to proceed.
protected function processEntity($data)
{
$em = $this->getDoctrine()->getEntityManager();
$metadata = $em->getClassMetadata(get_class($data));
$fqcn = $metadata->getName();
$blankEntity = new $fqcn();
$id = array();$this->get('logger')->debug('Incoming object type: ' . get_class($data)); # Keobi\ModelBundle\Entity\User $this->get('logger')->debug('Blank entity type: ' . get_class($blankEntity)); # Keobi\ModelBundle\Entity\User $this->get('logger')->debug('FQCN: ' . $fqcn); # Keobi\ModelBundle\Entity\User foreach ($metadata->getIdentifierFieldNames() as $identifier) { $id[$identifier] = $metadata->getFieldValue($data, $identifier); if (!$id[$identifier]) { $this->get('logger')->debug(sprintf("Identifer '%s' missing. Assuming the object is new.", $identifier)); return $data; } } $entity = $em->find($metadata->getName(), $id); $this->get('logger')->debug(get_class($entity)); # Keobi\RestAPIBundle\DefaultController foreach ($metadata->getFieldNames() as $field) { $value = $metadata->getFieldValue($data, $field); $default = $metadata->getFieldValue($blankEntity, $field); if ($value <> $default) { $metadata->setFieldValue($entity, $field, $value); # <- throws ErrorException $this->get('logger')->debug(sprintf("Updated field '%s' in %s", $field, get_class($entity))); } } return $entity;}
Looks like there was an issue with Doctrine2. I updated it to the latest HEAD commit of the Github repo. What puzzles me is that this method actually worked not too long ago.
Couldn’t find the exact commit that fixed the issue, but, as of this writing, the commit is 93cef612701b9e8caaf43c745978cd4fbd9d4e4e.