mongodb - 教义2 MongoDB获取对象的子记录

标签 mongodb symfony doctrine-orm doctrine-odm

我正在使用doctrine/mongodb-odm-bundle,但有一个问题:我无法从文档中获取引用的行(或者我只是不知道如何执行此操作..) 我有 2 个具有一对多引用的文档,如下所示: 首先

/**
 * @MongoDB\Document(collection="categories")
 */
class Category
{
    /**
     * @var integer $id
     *
     * @MongoDB\Id(strategy="auto")
     */
    private $id;

    /**
     * @var string $name
     *
     * @MongoDB\String
     * @Assert\NotBlank()
     * @Assert\MinLength(3)
     */
    private $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Application\Bundle\DefaultBundle\Document\Wallpaper", mappedBy="category")
     */
    private $files;
.................
    /**
     * Set files
     *
     * @param array $files
     */
    public function setFiles($files)
    {
        $this->files = $files;
    }

    /**
     * Get files
     *
     * @return array $files
     */
    public function getFiles()
    {
        return $this->files;
    }

........................
第二次

/**
 * @MongoDB\Document(collection="wallpapers")
 */
class Wallpaper
{
    /**
     * @var string $id
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;
    /**
     * @MongoDB\ReferenceOne(targetDocument="Application\Bundle\DefaultBundle\Document\Category", inversedBy="files")
     */
    private $category;

    /**
     * Get category
     *
     * @return Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set category
     *
     * @param Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

}

这是来自 Controller 的代码:

$category = $dm->getRepository('ApplicationDefaultBundle:Category')->findOneBy(...);
$wallpapers = $category->getFiles();

$wallpapers 和 $document->files 为 NULL。我如何检索与类别相关的记录?我如何从混凝土壁纸对象中获取类别?是否有像标准 ORM 中那样的“JOIN”模拟?

最佳答案

映射看起来正确。我认为您的问题可能与查询有关。我还会检查壁纸集合是否具有正确的文档,其中的类别字段填充了正确的 DBRef 对象数据。

$category = $dm->getRepository('Application\Bundle\DefaultBundle\Document\Wallpaper')->findOneById($id);
$wallpapers = $category->getFiles(); // Will return a cursor to the wallpaper objects
foreach ($wallpapers as $wallpaper) {
     do stuff
}

如果这不是问题,您能否粘贴您正在尝试的完整查询以及两个集合中的数据示例。

关于mongodb - 教义2 MongoDB获取对象的子记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791301/

相关文章:

linux - 无法在自定义 Linux 上运行 mongod

c# - ASP.NET core 中第三方数据上下文的依赖注入(inject)

php - 在 Symfony2 中创建表之前选择 DB Schema

caching - 如何将数据存储在 symfony2 的缓存中

entity-framework - 实体上列 'urbanization' 的重复定义

orm - fetch ="EAGER"和 fetch ="LAZY"在学说上有什么区别

javascript - 寻求帮助在 mongoose/mongodb 函数/方法中创建具有动态命名的干代码

mongodb - 如何在不创建 Mongoose 模型的情况下将 GraphQL 与 Mongoose 和 MongoDB 结合使用

php - 无法使用 Symfony 中的 Doctrine 连接到 mysql 数据库

html - Symfony 打印 a4 格式的标签