[MODM-139] [PATCH] @ReferenceMany with no referenceMapping cannot handle DBRef in all / in queries Created: 08/Apr/11  Updated: 08/Apr/11

Status: Open
Project: Doctrine MongoDB ODM
Component/s: Document Manager
Affects Version/s: 1.0.0BETA3
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Gérald Croes Assignee: Jonathan H. Wage
Resolution: Unresolved Votes: 1
Labels: None
Environment:

Linux, PHP 5.3



 Description   

I have thoose examples :

/** @Document 
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({"Test\Product"="Product", "Test\Year"="Year"})
*/
Attributes {
   /** @Id */
   protected $_id;
}

/** @Document */
class Product extends Attributes {}

/** @Document */
class Year extends Attributes {}

/**
 * Classe de base pour les différents contenus
 * @Document
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({"Test\Document"="Document", "Test\Infos"="Infos"})  
 */
class Content
{
   /** protected $_id */
    /** @ReferenceMany */
    protected $_attributes;
}

And the querying stuff

//and the query : 
$queryBuilder = $dm->createQueryBuilder('Test\DOcument');
//...
$attribute1Ref = $dm->createDBRef($attribute1Object);
$attribute2Ref = $dm->createDBRef($attribute2Object);
$queryBuilder->field('_attributes')->all(array($attribute1Ref, $attribute2Ref));
$queryBuilder->getQuery()->execute();//Won't work as the generated query won't specify the "_doctrine_class_name" value.

Proposed patch in DocumentManager :

    /**
     * Returns a DBRef array for the supplied document.
     *
     * @param mixed $document A document object
     * @param array $referenceMapping Mapping for the field the references the document
     *
     * @return array A DBRef array
     */
    public function createDBRef($document, array $referenceMapping = null)
    {
        $className = get_class($document);
        $class = $this->getClassMetadata($className);
        $id = $this->unitOfWork->getDocumentIdentifier($document);

        $dbRef = array(
            $this->cmd . 'ref' => $class->getCollection(),
            $this->cmd . 'id'  => $class->getDatabaseIdentifierValue($id),
            $this->cmd . 'db'  => $this->getDocumentDatabase($className)->getName()
        );

        // add a discriminator value if the referenced document is not mapped explicitely to a targetDocument
        if ($referenceMapping && ! isset($referenceMapping['targetDocument'])) {
            $discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name';
            $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName();
            $dbRef[$discriminatorField] = $discriminatorValue;
+        }  elseif ($referenceMapping === null) {
+            $dbRef['_doctrine_class_name'] = $class->getName();
+        }
        return $dbRef;
    }

Generated at Thu Aug 28 13:14:40 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.