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...