Doctrine ODM OneToOne Bi-Directional Reference using repositoryMethod Doctrine ODM OneToOne Bi-Directional Reference using repositoryMethod symfony symfony

Doctrine ODM OneToOne Bi-Directional Reference using repositoryMethod


Doctrine ODM will already lazy load the referenced document, rather than prefetch it for you.

I believe your problem actually lies in your repository methods... For example, in the findOneByMetaData function the first thing you are doing is calling $metadata->getArticle() in doing this you are asking doctrine to load the article from the database, which due to your repositoryMethod is going to call findOneByMetaData again. This is why you are seeing multiple queries.

Your findOneByMetaData function should look more like this:

// In the ArticleRepositorypublic function findOneByMetaData(ArticleMetaData $metadata){    $article = $this->createQueryBuilder()        ->field('groupcode')->equals($metadata->getGroupcode())        ->getQuery()        ->getSingleResult();    $article->setMetaData($metadata);    return $article;}

Doctrine will take care of whether the article has been loaded yet, so there is no need to try and check for a null value. The same also applies to your findOneByArticle function too.

Hope this make sense, and helps you resolve your issue.


This is because of Logger (loggableCursor), it duplicates queries in the log file.For example, you call ...find()->limit(1)->getQuery() it loggs each call but actually there is a single query request.

More info: https://github.com/doctrine/mongodb-odm/issues/471#issuecomment-63999514

ODM issue: https://github.com/doctrine/mongodb/issues/151