/** * @param PersistentCollection $collection * * @return Query */ public function createReferenceManyInverseSideQuery(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $owner = $collection->getOwner(); $ownerClass = $this->dm->getClassMetadata(get_class($owner)); $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); $mappedByMapping = isset($targetClass->fieldMappings[$mapping['mappedBy']]) ? $targetClass->fieldMappings[$mapping['mappedBy']] : array(); $mappedByFieldName = isset($mappedByMapping['simple']) && $mappedByMapping['simple'] ? $mapping['mappedBy'] : $mapping['mappedBy'] . '.$id'; $criteria = $this->cm->merge(array($mappedByFieldName => $ownerClass->getIdentifierObject($owner)), $this->dm->getFilterCollection()->getFilterCriteria($targetClass), isset($mapping['criteria']) ? $mapping['criteria'] : array()); $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); $qb = $this->dm->createQueryBuilder($mapping['targetDocument'])->setQueryArray($criteria); if (isset($mapping['sort'])) { $qb->sort($mapping['sort']); } if (isset($mapping['limit'])) { $qb->limit($mapping['limit']); } if (isset($mapping['skip'])) { $qb->skip($mapping['skip']); } if (!empty($hints[Query::HINT_SLAVE_OKAY])) { $qb->slaveOkay(true); } if (!empty($hints[Query::HINT_READ_PREFERENCE])) { $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); } return $qb->getQuery(); }
/** * Constructor. * * @param DocumentManager $dm */ public function __construct(DocumentManager $dm, UnitOfWork $uow) { $this->dm = $dm; $this->uow = $uow; $this->defaultPrimer = function (DocumentManager $dm, ClassMetadata $class, array $ids, array $hints) { $qb = $dm->createQueryBuilder($class->name)->field($class->identifier)->in($ids); if (!empty($hints[Query::HINT_SLAVE_OKAY])) { $qb->slaveOkay(true); } if (!empty($hints[Query::HINT_READ_PREFERENCE])) { $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); } $qb->getQuery()->execute()->toArray(false); }; }
/** * Create a new Query\Builder instance that is prepopulated for this document name * * @return Query\Builder $qb */ public function createQueryBuilder() { return $this->dm->createQueryBuilder($this->documentName); }