Search code examples
doctrine-ormdoctrine-odm

Why a function call in Doctrine's proxy object points to parent object instead of inherited child object?


I have created an inherited document class, see the code below. The documents persists fine but when fetching the document and trying to call the children's function, I will get an error Call to undefined method Proxies__CG__\Acme\ProductBundle\Document\ProductBase::getPriceDefinition() even the child document has getPriceDefinition() function. The proxy points also to the ProductBase, not SimpleProduct.

The parent class, ProductBase.php

<?php
namespace Acme\ProductBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 * @MongoDB\InheritanceType("SINGLE_COLLECTION")
 * @MongoDB\DiscriminatorField(fieldName="type")
 * @MongoDB\DiscriminatorMap({"simple"="SimpleProduct"})
 */
abstract class ProductBase
{   
    /**
     * @MongoDB\Id;
     */
    protected $_id;

    /**
     * @MongoDB\String
     */
    public $comment;
}

The child class, SimpleProduct.php

<?php
namespace Acme\ProductBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class SimpleProduct extends ProductBase
{

    /**
     * @var PriceDefinition
     * @MongoDB\EmbedOne(targetDocument="PriceDefinition") 
     */
    protected $priceDefinition;

    public function getPriceDefinition() {
        return $this->priceDefinition;
    }
}

Calling code (only partial):

$product = $this->dm->getRepository('AcmeProductBundle:Product')->findOneBy(array('_id' => $productId));
$priceDefinition = $product->getPriceDefinition(); // The error is thrown here

Finally the document in the database (that is persisted with the above documents correctly).

{
  "_id": ObjectId("5006d7b76803fa9403000007"),
  "priceDefinition": {
     "referenceValue": 1000000,
     "currency": "iso: EUR",
     "taxBehavior": "fi_vat_a",
     "isGrossPrice": false
  },
  "type": "simple"
}   

In general this somehow seems that Doctrine would not recognize that the returned object is a subclass. But - if I for example change the database type value to something else than "simple" (for example "not-simple") that does not match the DiscriminatorMap, the Doctrine says "Notice: Undefined index: not-simple (...)".

And what the strangest, as soon as I add getPriceDefinition() in the parent class (ProductBase), it gets called properly and without errors.

Uhh... that was a long question. Anyhow, I am just unable to find out what's the problem here. Is it my getRepository('AcmeProductBundle:Product') call, or my inheritance definitions...


Solution

  • Given that ProductBase is abstract, is there any reason you're not annotating it as a MappedSuperclass? ODM is constructing a proxy that extends ProductBase (it does so between your findOneBy() call and attempting to call getPriceDefinition()), which doesn't make sense in practice. Proxy objects are meant to be used interchangeably with the mapped class -- in this case, the proxy is extending an abstract class which should never exist on its own. Even so, it seems like this odd mapping is causing a bug in the discriminator logic, as that type field should cause SimpleProduct to be used.

    Also, changing the database value of the type field to not-simple should not cause the code to elicit a PHP notice. We should be throwing an exception instead.

    Would you mind opening separate issues for both of these bugs in the GitHub repository? If you're feeling up to it, failing test cases would be welcome as well.