JMSSerializerBundle から逆シリアル化されたオブジェクトを取得し、完全なオブジェクトを返すメソッドがコントローラー内にあり、永続化の準備ができています。私がこれを行っている理由は、更新されたオブジェクトを返すためです。そのため、永続化されたときに、すべてがデフォルトにリセットされるわけではありません。
実行すると、このメソッドは指定された行に をスローし、ErrorException
これをログに記録します。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
何らかの理由で、EntityManager
メソッドは、このメソッドのコントローラー クラスであるfind
(によるとget_class
)を返しています。DefaultController
そしてそれsetFieldValue
はNULLだと言います。
私を混乱させていることの1つはDoctrine\ORM\EntityNotFoundException
、エンティティが見つからない場合にスローする必要があることです。投げられていません。したがって、エンティティを適切に返していないことは明らかです...
受信パラメータ$data
は JSMSerializerBundle によってデシリアライズされたオブジェクトです。$em
間違いなく間違いEntityManager
あり$metadata
ませんClassMetadata
。
私はここで途方に暮れています。進め方がわからない。
protected function processEntity($data)
{
$em = $this->getDoctrine()->getEntityManager(); $metadata = $em->getClassMetadata(get_class($data)); $fqcn = $metadata->getName(); $blankEntity = 新しい $fqcn(); $id = 配列();$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;
}