[MODM-123] Single Collection Inheritance : hydration won't work properly Created: 17/Feb/11  Updated: 23/Feb/11  Resolved: 23/Feb/11

Status: Resolved
Project: Doctrine MongoDB ODM
Component/s: None
Affects Version/s: 1.0.0BETA2
Fix Version/s: 1.0.0BETA2

Type: Bug Priority: Blocker
Reporter: Billy Bob Assignee: Jonathan H. Wage
Resolution: Fixed Votes: 0
Labels: None
Environment:

Mac OS X, MAMP



 Description   

There is still a problem with Single Collection Inheritance.

Please consider the following code :

 
/**
 * @Document(db="forum", collection="Items")
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({0="Items", 1="Stuffs"})
*/
class Items
{
    /**
     *
     * @id
     */
    public $id;

    /**
     * @String
     */
    public $title;
}



/**
 * @Document(db="forum", collection="Items")
*/
class Stuffs extends Items
{
    
    /**
     *
     * @id
     */
    public $id;

    /**
     * @String
     */
    public $stuffname;

}

$a = new Stuffs();
$a->stuffname = 'TTT';
$dm->persist($a);
$dm->flush();

Resulting in the database with :

 
{
   "_id": ObjectId("4d5dd082b53251b84e000000"),
   "stuffname": "TTT",
   "type": 1 
}

Now if I do :

 
$result = $dm->find('Items', $a->id);

then $result is an instance of Stuffs

But if I try :

 
$result = $dm->find('Items', '4d5dd082b53251b84e000000'); 

then $result is an instance of Items

Note that '$a->id' is a string, the same string containing '4d5dd082b53251b84e000000', so :

 
$result = $dm->find('Items', $a->id); 
$result = $dm->find('Items', '4d5dd082b53251b84e000000'); 

are semantically equivalent. It seems however that hydration would ignore the DiscriminatorMap in the second example.

However If you do :

 
$result = $dm->find('Stuffs', '4d5dd082b53251b84e000000'); 

You'll get a Stuffs as a result.

Now I tried to find a workaround when you don't know the type of the returned object. My initial intention was to first fetch a document with :

 
$result = $dm->find('Items', '4d5dd082b53251b84e000000'); 

Then I'm testing a property in the objet to see if it's a Stuffs, if it is, I tried an new :

 
$result = $dm->find('Stuffs', '4d5dd082b53251b84e000000'); 

In order to fetch a Stuffs. But ... I still get a *Items"!

So basically, if you do :

 
$result = $dm->find('Stuffs', '4d5dd082b53251b84e000000'); 

You get a Stuffs. But if you do :

 
$dm->find('Items', '4d5dd082b53251b84e000000'); 
$result = $dm->find('Stuffs', '4d5dd082b53251b84e000000'); 

You get a Items!



 Comments   
Comment by Jonathan H. Wage [ 18/Feb/11 ]

Hi, can you make a phpunit testcase here? https://github.com/doctrine/mongodb-odm/tree/master/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket

Comment by Billy Bob [ 18/Feb/11 ]

Sorry but I'm not familiar with phpunit.

If that can help, I've made more tests here. Strangely when I deleted all the documents in the test base and created new ones, the hydration then worken properly! However in my real project, the issue is still here ...

In conclusion it seems that the issue is random.

Comment by Jonathan H. Wage [ 19/Feb/11 ]

I am not able to produce the issue here. Can you make a executable test that I can run so that I can see what you see?

Comment by Billy Bob [ 23/Feb/11 ]

Doesn't seem to be an issue in BETA2.

Generated at Sun Dec 21 00:28:55 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.