繁体   English   中英

我无法在Symfony中从学说odm中检索嵌入式文档

[英]I can't retrieve embedded documents from doctrine odm in Symfony

我有一个带有多个“ 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"
}

检索所有embedMany映射作为延迟加载的持久性集合。 根据您生成上述输出的方式,这将解释您为何认为其为空的原因。 在集合上运行counttoArray应该返回正确的项目数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM