直升机,
首先,请原谅我的英语不太好。
我正在将 Symfony2 应用程序的数据容器迁移到 MongoDB,然后它与 MySQL 一起运行。
我添加了 DoctrineMongoDBBundle 并且“几乎所有东西”都能完美运行。
我有一些文档之间的引用,我想在其中保留 Doctrine ORM 提供的“延迟加载”模式。我已经阅读了 Doctrine ODM 的官方文档,
以及一些解释如何创建关系和定义文档以获得“延迟加载”行为的示例,
但我不能让它工作。
就我而言,我有两个文件,“旅行”和“笔记”,我想保持 1:N 的关系,如下所示:
<?php
namespace MyApp\TravelBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Travel
*
* @ODM\Document(collection="travel")
*/
class Travel {
/**
* @var \MyApp\NoteBundle\Document\Note
*
* @ODM\ReferenceMany(targetDocument="\MyApp\NoteBundle\Document\Note", mappedBy="travel", sort={"createdAt"="asc"} )
*/
private $notes;
// more properties ...
public function __construct() {
$this->notes = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add notes
*
* @param \MyApp\NoteBundle\Document\Note $notes
*/
public function addNote(\MyApp\NoteBundle\Document\Note $notes) {
$this->notes[] = $notes;
}
/**
* Remove notes
*
* @param \MyApp\NoteBundle\Document\Note $notes
*/
public function removeNote(\MyApp\NoteBundle\Document\Note $notes) {
$this->notes->removeElement($notes);
}
/**
* Get notes
*
* @return Doctrine\Common\Collections\Collection $notes
*/
public function getNotes() {
return $this->notes;
}
// more methods ...
}
?>
<?php
namespace MyApp\NoteBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Note
*
* @ODM\Document(collection="note")
*/
class Note
{
/**
* @var \MyApp\TravelBundle\Document\Travel
*
* @ODM\ReferenceOne(targetDocument="MyApp\TravelBundle\Document\Travel", inversedBy="notes")
*/
private $travel;
// more properties ...
/**
* Set travel
*
* @param \MyApp\TravelBundle\Document\Travel $travel
* @return Note
*/
public function setTravel(\MyApp\TravelBundle\Document\Travel $travel) {
$this->travel = $travel;
$travel->addNote($this);
return $this;
}
// more methods ...
}
?>
当我在旅行中添加注释时,我了解旅行证件的结果应该是:
{ "_id" : ObjectId( "5183aa63095a1a3921000000" ),
"name" : "First travel",
"isActive" : true,
"createdAt" : Date( 1367583331000 ),
"updatedAt" : Date( 1367583331000 ),
"notes" : [{ "$ref" : "note",
"$id" : ObjectId( "5183aa63095a1a3955000000" ),
"$db" : "mydb" }]
}
对于注释文件应该是:
{ "_id" : ObjectId( "5183aa63095a1a3955000000" ),
"travel" : { "$ref" : "travel",
"$id" : ObjectId( "5183aa63095a1a3921000000" ),
"$db" : "mydb" },
"note" : "First note",
"createdAt" : Date( 1367583331000 ),
"updatedAt" : Date( 1367583331000 ) }
但现在我只在便笺文件中得到参考,而旅行文件中没有参考,当我在旅行文件中查询时,Doctrine 不加载相关的便笺文件:
<?php
.
.
$travel = $dm->getRepository('TravelBundle:Travel')->findCurrentTravel($user->getId());
$travel->getNotes(); // IS EMPTY :(
.
.
?>
我为旅行添加注释的过程如下:
<?php
namespace MyApp\TravelBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class TravelController extends Controller {
public function createNoteAction(Request $request) {
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$travel = $dm->getRepository('TravelBundle:Travel')->findCurrentTravel($user->getId());
$entity = new Note();
$form = $this->createForm(newNoteType(), $entity);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$entity->setTravel($travel);
$dm>persist($travel);
$dm>persist($entity);
$dm>flush();
}
}
}
}
?>
任何获取 $travel->getNotes() 方法的想法或建议都可以通过“延迟加载”自动检索引用的笔记。
非常感谢您的贡献,
萨卡里亚斯