使用文档管理器(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;
// ...
我错了什么?或者这是一个错误?或者......水合物功能不是“外部”使用吗? 希望有人能帮助我
此致 诺曼