Skip to content

Indexes missing / Single Collection Inheritance #465

Open
kevou84 opened this Issue Dec 16, 2012 · 12 comments

5 participants

@kevou84
kevou84 commented Dec 16, 2012

Hi all,

I'm doing Single Collection Inheritance like this :

 @MongoDB\Document(
      collection="baseobjects",
      repositoryClass="PUC\CommonBundle\Repository\BaseObjectRepository",
      indexes={
         @MongoDB\Index(keys={"type"="asc"}),
      },
      requireIndexes=true
 )
 @MongoDB\InheritanceType("SINGLE_COLLECTION")
 @MongoDB\DiscriminatorField(fieldName="type")
 @MongoDB\DiscriminatorMap({
      "child" = "Child",
      ...
 })

class BaseObject
...


 @MongoDB\Document(
      collection="child",
      repositoryClass="PUC\CommonBundle\Repository\ChildRepository",
      indexes={
         @MongoDB\Index(keys={"email"="asc"})         
      },
      requireIndexes=true
 )

class Child extends BaseObject
...

Child indexes are not generated at BaseObject level (since Mongo 2.2.2) when using doctrine:mongodb:schema:update/create command.

Since I require Indexes when querying Child objects (requireIndexes=true) it triggers an error : "Cannot execute unindexed queries on TEST\MyBundle\Document\Child. Unindexed fields: email"

I noticed that children collections are not physically created anymore in db since 2.2.2, which seems fair, but then the indexes shall be on mother class (which is stored), isn't it ? If I create one manually on mother class it works fine.

Thanks you for your feedback,

Cheers

@kevou84
kevou84 commented Dec 22, 2012

The problem seems to occur only if specifying Bundle/Document not when using the raw command.

@smilesrg

+1 for this issue, I have the same too.

@smilesrg

Damn! This issue was opened at Dec, 2012! Now it's the end of July, 2014. Why nobody fixed this for these two years?

@jmikola jmikola added this to the 1.0.0 milestone Jul 24, 2014
@jmikola jmikola added the bug label Jul 24, 2014
@jmikola
Doctrine member
jmikola commented Jul 24, 2014

Child indexes are not generated at BaseObject level (since Mongo 2.2.2) when using doctrine:mongodb:schema:update/create command.

It's not clear to me why the database version is significant here.

The problem seems to occur only if specifying Bundle/Document not when using the raw command.

Does that mean it's specific to DoctrineMongoDBBundle, and not ODM?

@malarzm malarzm self-assigned this Jul 30, 2015
@malarzm
Doctrine member
malarzm commented Aug 8, 2015

The thing that strikes me odd is that example's BaseObject is using collection baseobjects, declaring @MongoDB\InheritanceType("SINGLE_COLLECTION") and yet Child specifies collection child. @kevou84 (or @smilesrg as you were complaining as well yet haven't provided any details) would you mind shedding more light on the issue? Retriaging as a question until then.

@malarzm malarzm added question and removed bug labels Aug 8, 2015
@malarzm malarzm removed this from the 1.0.0 milestone Aug 8, 2015
@malarzm malarzm removed their assignment Aug 8, 2015
@smilesrg
smilesrg commented Aug 9, 2015

@malarzm I faced with this issue a year ago, and I don;t remember now what exactly was wrong. As far as I remember, indexes from BaseObject is not generated but they should.

@malarzm
Doctrine member
malarzm commented Aug 9, 2015

@smilesrg thanks, I'll try to write a failing test case or this

@malarzm
Doctrine member
malarzm commented Aug 9, 2015

Ok, so I produced this test case:

<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

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

class GH465Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
    /**
     * @dataProvider provideClass
     */
    public function testCreatingIndexesForBaseDocument($class)
    {
        $indexes = $this->dm->getSchemaManager()->getDocumentIndexes($class);

        $this->assertCount(2, $indexes);

        $this->assertTrue(isset($indexes[0]['keys']['int']));
        $this->assertEquals(1, $indexes[0]['keys']['int']);

        $this->assertTrue(isset($indexes[1]['keys']['float']));
        $this->assertEquals(1, $indexes[1]['keys']['float']);
    }

    public function provideClass()
    {
        return array(
            array(__NAMESPACE__ . '\\GH465Base'),
            array(__NAMESPACE__ . '\\GH465Child'),
        );
    }
}

/**
 * @ODM\Document
 * @ODM\Index(keys={"int"="asc"})
 * @ODM\InheritanceType("SINGLE_COLLECTION")
 * @ODM\DiscriminatorField(fieldName="type")
 * @ODM\DiscriminatorMap({
 *  "child" = "GH465Child"
 * })
 */
class GH465Base
{
    /** @ODM\Id */
    public $id;

    /** @ODM\Int */
    public $int;
}

/**
 * @ODM\Document
 * @ODM\Index(keys={"float"="asc"})
 */
class GH465Child extends GH465Base
{
    /** @ODM\Float) */
    public $float;
}

and indeed indexes defined in GH465Child are not created when creating indexes for GH465Base. I also think that this is technically correct since we are generating indexes for a document and not for a whole collection.

On the other hand we could iterate through discriminatorMap and gather all indexes in SchemaManager but that would be a feature in my opinion :)

@malarzm malarzm added feature and removed question labels Aug 9, 2015
@smilesrg
smilesrg commented Aug 9, 2015

@malarzm does indexes that defined in GH465Base created when creating indexes for GH465Child?

@malarzm
Doctrine member
malarzm commented Aug 9, 2015

@smilesrg yes they did, only test with GH465Base provided is failing

@getvivekv

4 years and nobody fixed this

@malarzm
Doctrine member
malarzm commented Jan 16, 2016

@getvivekv nobody stops you from doing so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.