我有一个带有多个“PageContent”嵌入文档的“页面”文档。当我恢复“页面”时,属性“内容”为空。如果我没有水合结果数据,那么当我给它们保湿时,它们会消失。
我阅读了我发现的所有内容,但我没有看到我在每个工作示例中做了任何不同的事情。
请帮帮我。我对这一切都很疯狂。
页面文件:
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document(collection="pages")
*/
class Page
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $name;
/**
* @MongoDB\Field(type="string")
*/
protected $slug;
/**
* @MongoDB\EmbedMany(targetDocument="\AppBundle\Document\PageContent")
*/
protected $contents = array();
/**
* @MongoDB\Field(type="date")
*/
protected $createdAt;
/**
* @MongoDB\Field(type="date")
*/
protected $updatedAt;
/**
* @MongoDB\Field(type="string")
*/
protected $author;
public function __toString()
{
return $this->getName();
}
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string $name
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return $this
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string $slug
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set createdAt
*
* @param date $createdAt
* @return $this
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return date $createdAt
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param date $updatedAt
* @return $this
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return date $updatedAt
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set author
*
* @param string $author
* @return $this
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string $author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add content
*
* @param AppBundle\Document\PageContent $content
*/
public function addContent(\AppBundle\Document\PageContent $content)
{
$this->contents[] = $content;
}
/**
* Remove content
*
* @param AppBundle\Document\PageContent $content
*/
public function removeContent(\AppBundle\Document\PageContent $content)
{
$this->contents->removeElement($content);
}
/**
* Get contents
*
* @return \Doctrine\Common\Collections\Collection $contents
*/
public function getContents()
{
return $this->contents;
}
}
页面内容文档:
<?php
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\EmbeddedDocument
*/
class PageContent
{
/**
* @MongoDB\Field(type="string")
*/
protected $type;
/**
* @MongoDB\Field(type="string")
*/
protected $content;
/**
* Set type
*
* @param string $type
* @return $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string $type
*/
public function getType()
{
return $this->type;
}
/**
* Set content
*
* @param string $content
* @return $this
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string $content
*/
public function getContent()
{
return $this->content;
}
}
在控制器中:
$dm = $this->get('doctrine_mongodb')->getManager();
$document = $dm->createQueryBuilder('AppBundle:Page')
->field('id')->equals('someid')
->hydrate(false)
->getQuery()
->getSingleResult();
这会正确返回数据:
array:7 [▼
"_id" => MongoId {#439 ▶}
"name" => "contenido 3"
"slug" => "contenido-3"
"contents" => array:1 [▼
0 => array:2 [▼
"type" => "html"
"content" => "some content"
]
]
"createdAt" => MongoDate {#440 ▶}
"updatedAt" => MongoDate {#441 ▶}
"author" => "adminpassb"
]
但这不是:
$dm = $this->get('doctrine_mongodb')->getManager();
$document = $dm->createQueryBuilder('AppBundle:Page')
->field('id')->equals('someid')
->hydrate(true)
->getQuery()
->getSingleResult();
Page {#424 ▼
#id: "57d7cdb15d6361ef0c3c986a"
#name: "contenido 3"
#slug: "contenido-3"
#contents: []
#createdAt: DateTime {#428 ▶}
#updatedAt: DateTime {#430 ▶}
#author: "adminpassb"
}
答案 0 :(得分:0)
所有embedMany
映射都被检索为持久的集合,这些集合是懒惰加载的。根据您生成上述输出的方式,这可以解释为什么您认为它是空的。在集合上运行count
或toArray
应返回正确数量的项目。