我使用文档管理器(zend framework 2 应用程序)的水合物功能将数组数据水合到文档中。不幸的是,嵌入的子文档不会在数据库中更新(但主文档会)。
数据的水合是正确的,但是工作单元不会“注意到”子文档的更改。
例子:
请求 1...(创建文档)
$user = new Document\User();
$user->setName('administrator');
$address = new Document\UserAddress();
$address->setCity('cologne');
$user->setAddress($address);
// will insert new document correctly
$documentManager->persist($user);
$documentManager->flush();
请求 2...(更新现有文档)
$repository = $this->documentManager->getRepository('Document\User');
$user = $repository->findOneBy(
array('name' => 'administrator')
);
$documentManager->getHydratorFactory()->hydrate(
$user,
array(
'name' => 'administrator',
'address' => array(
'city' => 'bonn' // change "cologne" to "bonn"
)
)
// note: "address" array will be correctly hydrated to a "UserAddress" document
// this will update the "User" document, but not the embedded "UserAddress" document
$documentManager->persist($user);
$documentManager->flush();
对我来说有点奇怪,但是分离子文档会导致更新
// after detaching sub object the documents will be saved correctly
// (but this cant be the way it should be...?)
$documentManager->detach($user->getUserAddress());
$documentManager->persist($user);
$documentManager->flush();
以下是注释:
/*
* @ODM\Document(collection="Users")
*/
class User
{
/**
* @ODM\EmbedOne(targetDocument="Document\UserAddress")
*/
protected $address;
//...
/*
* @ODM\EmbeddedDocument()
*/
class UserAddress
{
/**
* @ODM\String
*/
protected $city;
// ...
我错了什么?或者这是一个错误?或者……水合物功能不适合“外用”吗?希望可以有人帮帮我
问候诺曼