doctrine:mongodb:fixtures:load removes created indexes

833 Views Asked by At

I am using Symfony 2 with the Doctrine Mongodb ODM, and trying to create some indexes that work together with the fixtures I have already created and am trying to load with

  php -f app/console doctrine:mongodb:fixtures:load

When I create indexes on my documents, loading the fixtures fails with

  [Doctrine\ODM\MongoDB\MongoDBException]                                                                   
  Cannot execute unindexed queries on Updatemi\LibraryBundle\Document\UpdateTerm. Unindexed fields: update

Does ODM not generate the Indexes by itself?

1

There are 1 best solutions below

0
On

After some research I found a solution to this problem.

Doctrine does not create the indexes automatically, after they are deleted, you have to do this yourself, using the command

sf doctrine:mongodb:schema:create --index 

This does not solve the problem for the fixtures, since they delete everything before they execute their code.

But there is a solution for this problem too!

At the start of your fixtures file, add a line that regenerates the indexes for you.

    $dm = $this->container->get('doctrine.odm.mongodb.document_manager');
    $dm->getSchemaManager()->ensureIndexes();

There is also a pull request on the doctrine_fixtures library to avoid deleting the indexes on fixture loading, so this fix hopefully is not necessary anymore.

https://github.com/doctrine/data-fixtures/pull/118