How to request embedded documents with a reference document?

I am using Doctrine ODM and am having trouble querying embedded documents using a referenced document.

Consider the following documents:

<?php

/** @Document */
class TopCategory 
{

    /** EmbedMany(targetDocument="SubCategory") */
    private $subCategories;

}

/** @EmbeddedDocument */
class SubCategory 
{

    /** ReferenceMany(targetDocument="Product") */
    private $products;

}


/** @Document */
class Product
{

    /** @String */
    private $name;

}

Now I'm wondering how I can find TopCategory (or SubCategory) by product, I tried several different ways to achieve this, one method works, but a little hacky.

The first method does not work:

$category = $dm->createQueryBuilder('TopCategory')
    ->field('subCategories.products')->includesReferenceTo($someProduct)
    ->getQuery()->execute();
// ... gives Doctrine\ODM\MongoDB\MongoDBException: No mapping found for field 'subCategories.products' in class 'TopCategory'.'

The second method does not work:

$category = $dm->createQueryBuilder('SubCategory')
    ->field('products')->includesReferenceTo($someProduct)
    ->getQuery()->execute();    
// ... returns null

Third, a workaround:

$category = $dm->createQueryBuilder('SubCategory')
    ->field('products.$id')->equals(new \MongoId($someProduct->getId()))
    ->getQuery()->execute();    
// .. works, but seems hackish

I am using the latest from GitHub and MognoDB v1.8.0 What is connected with this?

NOTE. I wonder how Doctrine ODM allows you to directly return an embedded document.

+3
source share
1 answer

ReferenceMany ReferenceOne, - , , mongodb :

{
  $id: 'id',
  $db: 'referenced_doc_db_name',
  $ref: 'referenced_doc_collection_name'
}

ReferenceOne, ReferenceMany, , - , , .

, hackish;):

$category = $dm->createQueryBuilder('SubCategory')
    ->field('products.$id')->equals(new \MongoId($someProduct->getId()))
    ->getQuery()->execute();    
// .. works, but seems hackish

- ( id), embedOne embedMany .

+7

All Articles