ドキュメントマネージャー(zend Framework 2アプリケーション)のハイドレート機能を使用して、配列データをドキュメントにハイドレートします。残念ながら、埋め込まれたサブ文書はデータベースで更新されません (ただし、メイン文書は更新されます)。
データのハイドレーションは正しいですが、サブドキュメントの変更は Unit-Of-Work によって「認識」されません。
例:
リクエスト 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;
// ...
私は何を間違っていますか?それともこれはバグですか?それとも…ハイドレート機能は「外用」ではないのでしょうか?誰かが私を助けてくれることを願っています
よろしくノーマン