1

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;  

}

4

2 に答える 2

1

Doctrine2 に問題があったようです。Github repoの最新の HEAD commit に更新しました。私を困惑させているのは、この方法が実際に機能したのはそれほど前ではないということです。

問題を修正した正確なコミットは見つかりませんでしたが、この記事の執筆時点では、コミットは93cef612701b9e8caaf43c745978cd4fbd9d4e4eです。

于 2012-07-18T18:28:17.777 に答える
0
$entity = $em->find($metadata->getName(), $id);

次のように、検索するリポジトリを指定する必要があります。

$entity = $em->getRepository('EntitiesBundle:Entity')->find(...)
于 2012-07-18T17:59:18.130 に答える