我正在编写一个 Symfony2 应用程序,它允许移动用户通过 REST 服务创建和更新“家庭”。我使用 MongoDB 作为存储层,使用 Doctrine MongoDB ODM 进行文档处理。
和方法工作正常GET /homes/{key}
。POST /homes
当我尝试使用PUT /homes/{key}
.
这是当前代码:
/**
* PUT /homes/{key}
*
* Updates an existing Home.
*
* @param Request $request
* @param string $key
* @return Response
* @throws HttpException
*/
public function putHomeAction(Request $request, $key)
{
// check that the home exists
$home = $this->getRepository()->findOneBy(array('key' => (int) $key));
// disallow create via PUT as we want to generate key ourselves
if (!$home) {
throw new HttpException(403, 'Home key: '.$key." doesn't exist, to create use POST /homes");
}
// create object graph from JSON string
$updatedHome = $this->get('serializer')->deserialize(
$request->getContent(), 'Acme\ApiBundle\Document\Home', 'json'
);
// replace existing Home with new data
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$home = $dm->merge($updatedHome);
$dm->flush();
$view = View::create()
->setStatusCode(200)
->setData($home);
$response = $this->get('fos_rest.view_handler')->handle($view);
$response->setETag(md5($response->getContent()));
$response->setLastModified($home->getUpdated());
return $response;
}
JMSSerializer 将传递给操作的 JSON 字符串成功反序列化为我的 Document 对象图,但是当我尝试合并和刷新时,出现错误:
Notice: Undefined index: in ..../vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php line 1265
我一直在尝试遵循此处的文档:http: //docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/working-with-objects.html#merging-documents
在尝试合并之前,我需要对反序列化的 Home 做些什么吗?合并是错误的方法吗?
谢谢。