本文整理汇总了PHP中Doctrine\ORM\QueryBuilder::getRootEntities方法的典型用法代码示例。如果您正苦于以下问题:PHP QueryBuilder::getRootEntities方法的具体用法?PHP QueryBuilder::getRootEntities怎么用?PHP QueryBuilder::getRootEntities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\QueryBuilder
的用法示例。
在下文中一共展示了QueryBuilder::getRootEntities方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addJoins
/**
* Add completeness joins to query builder
*
* @param string $completenessAlias the join alias
* @param string $locale the locale
* @param sting $scope the scope
*
* @return CompletenessJoin
*/
public function addJoins($completenessAlias, $locale, $scope)
{
$rootAlias = $this->qb->getRootAlias();
$localeAlias = $completenessAlias . 'Locale';
$channelAlias = $completenessAlias . 'Channel';
$rootEntity = current($this->qb->getRootEntities());
$completenessMapping = $this->qb->getEntityManager()->getClassMetadata($rootEntity)->getAssociationMapping('completenesses');
$completenessClass = $completenessMapping['targetEntity'];
$this->qb->leftJoin('PimCatalogBundle:Locale', $localeAlias, 'WITH', $localeAlias . '.code = :cLocaleCode')->leftJoin('PimCatalogBundle:Channel', $channelAlias, 'WITH', $channelAlias . '.code = :cScopeCode')->leftJoin($completenessClass, $completenessAlias, 'WITH', $completenessAlias . '.locale = ' . $localeAlias . '.id AND ' . $completenessAlias . '.channel = ' . $channelAlias . '.id AND ' . $completenessAlias . '.product = ' . $rootAlias . '.id')->setParameter('cLocaleCode', $locale)->setParameter('cScopeCode', $scope);
return $this;
}
示例2: resetBuilder
protected function resetBuilder()
{
$rootEntities = $this->qb->getRootEntities();
$this->qb = $this->qb->getEntityManager()->getRepository($rootEntities[0])->createQueryBuilder('x');
$this->joinAliases = [];
$this->parametersMap = [];
$this->orderBy = [];
}
示例3: addJoins
/**
* Add completeness joins to query builder
*
* @param string $completenessAlias the join alias
* @param string $locale the locale
* @param string $scope the scope
*
* @return CompletenessJoin
*/
public function addJoins($completenessAlias, $locale, $scope)
{
$rootAlias = $this->qb->getRootAliases()[0];
$localeAlias = $completenessAlias . 'Locale';
$channelAlias = $completenessAlias . 'Channel';
$rootEntity = $this->qb->getRootEntities()[0];
$completenessMapping = $this->qb->getEntityManager()->getClassMetadata($rootEntity)->getAssociationMapping('completenesses');
$completenessClass = $completenessMapping['targetEntity'];
$joinCondition = sprintf('%s.product = %s.id', $completenessAlias, $rootAlias);
$this->qb->leftJoin('PimCatalogBundle:Channel', $channelAlias, 'WITH', $channelAlias . '.code = :cScopeCode')->setParameter('cScopeCode', $scope);
$joinCondition .= sprintf(' AND %s.channel = %s.id', $completenessAlias, $channelAlias);
if (null !== $locale) {
$this->qb->leftJoin('PimCatalogBundle:Locale', $localeAlias, 'WITH', sprintf('%s.code = :%scLocaleCode', $localeAlias, $localeAlias))->setParameter($localeAlias . 'cLocaleCode', $locale);
$joinCondition .= sprintf(' AND %s.locale = %s.id', $completenessAlias, $localeAlias);
}
$this->qb->leftJoin($completenessClass, $completenessAlias, 'WITH', $joinCondition);
return $this;
}
示例4: __construct
/**
* @param QueryBuilder $queryBuilder
* @param QueryBuilderTools $qbTools
*/
public function __construct(QueryBuilder $queryBuilder, QueryBuilderTools $qbTools)
{
// make sure 'from' DQL part is initialized for both original and optimized query builders
$queryBuilder->getRootEntities();
$this->originalQueryBuilder = $queryBuilder;
$this->qbTools = $qbTools;
// initialize the query builder helper
$this->qbTools->prepareFieldAliases($this->originalQueryBuilder->getDQLPart('select'));
$this->qbTools->prepareJoinTablePaths($this->originalQueryBuilder->getDQLPart('join'));
}
示例5: hasRootEntityWithIdentifier
/**
* Detects if the root entity has the given identifier.
*
* @param QueryBuilder $queryBuilder
* @param ManagerRegistry $managerRegistry
* @param bool $isForeign
*
* @return bool
*/
private static function hasRootEntityWithIdentifier(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry, bool $isForeign) : bool
{
foreach ($queryBuilder->getRootEntities() as $rootEntity) {
$rootMetadata = $managerRegistry->getManagerForClass($rootEntity)->getClassMetadata($rootEntity);
if ($isForeign ? $rootMetadata->isIdentifierComposite : $rootMetadata->containsForeignIdentifier) {
return true;
}
}
return false;
}
示例6: hasRootEntityWithForeignKeyIdentifier
/**
* Determines whether the query builder has any root entity with foreign key identifier.
*
* @param QueryBuilder $queryBuilder
* @param ManagerRegistry $managerRegistry
*
* @return bool
*/
public static function hasRootEntityWithForeignKeyIdentifier(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry)
{
foreach ($queryBuilder->getRootEntities() as $rootEntity) {
$rootMetadata = $managerRegistry->getManagerForClass($rootEntity)->getClassMetadata($rootEntity);
if ($rootMetadata->containsForeignIdentifier) {
return true;
}
}
return false;
}
示例7: getAllClassesForQueryBuilder
public function getAllClassesForQueryBuilder(QueryBuilder $queryBuilder) : array
{
$classes = [];
$rootEntities = $queryBuilder->getRootEntities();
foreach ($rootEntities as $rootEntity) {
$classes[$rootEntity] = $rootEntity;
$metadata = $this->getClassMetadata($rootEntity);
$this->addAssociationsTargetClasses($metadata, $classes);
}
return $classes;
}
示例8: ensureInitialized
/**
* Ensure that filtering applied and query builder wrapped in buffered iterator
* if data source is query builder or just an entity name
*
* @param ContextInterface $context
*/
protected function ensureInitialized(ContextInterface $context)
{
if (null !== $this->qb) {
if ($context->hasOption(self::ID_FILTER)) {
$optionValue = $context->getOption(self::ID_FILTER);
$em = $this->qb->getEntityManager();
$entityNames = $this->qb->getRootEntities();
$classMetadata = $em->getClassMetadata(reset($entityNames));
$identifier = $classMetadata->getSingleIdentifierFieldName();
if (is_array($optionValue)) {
$this->qb->andWhere($this->qb->expr()->in('o.' . $identifier, ':id'));
$this->qb->setParameter('id', $optionValue);
} else {
$this->qb->andWhere('o.' . $identifier . ' = :id');
}
$this->qb->setParameter('id', $optionValue);
}
$this->setSourceIterator(new BufferedQueryResultIterator($this->qb));
}
}
示例9: addJoinsToQueryBuilder
private function addJoinsToQueryBuilder($alias, QueryBuilder $qb, $className, $recursive = true)
{
foreach ($this->getEntityManager()->getClassMetadata($className)->getAssociationMappings() as $assoc) {
if (in_array($assoc['targetEntity'], $qb->getRootEntities()) || $className === $assoc['targetEntity']) {
continue;
}
$uniqueJoinAlias = $this->getUniqueAlias($assoc['targetEntity'], $qb);
$qb->addSelect($uniqueJoinAlias)->leftJoin(sprintf('%s.%s', $alias, $assoc['fieldName']), $uniqueJoinAlias);
if ($recursive) {
$this->addJoinsToQueryBuilder($uniqueJoinAlias, $qb, $assoc['targetEntity']);
}
}
}
示例10: setDefaultIdentifierFieldNames
/**
* Defines default identifiers from query builder in order to optimize count queries.
*
* @return $this
*
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
*/
public function setDefaultIdentifierFieldNames()
{
//Default identifier for table rows
$rootEntity = $this->queryBuilder->getRootEntities()[0];
$metadata = $this->queryBuilder->getEntityManager()->getMetadataFactory()->getMetadataFor($rootEntity);
$identifiers = array();
foreach ($metadata->getIdentifierFieldNames() as $identifierFieldName) {
$identifiers[] = $this->getAlias() . '.' . $identifierFieldName;
}
$rootEntityIdentifier = implode(',', $identifiers);
$this->setIdentifierFieldNames($rootEntityIdentifier ?: null);
return $this;
}
示例11:
function it_adds_a_filter_on_a_field_in_the_query(QueryBuilder $qb, Expr $expr, EntityManager $em, ClassMetadata $cm, Expr\Comparison $comparison)
{
$qb->expr()->willReturn($expr);
$qb->getRootAlias()->willReturn('p');
$qb->getRootEntities()->willReturn([]);
$qb->getEntityManager()->willReturn($em);
$em->getClassMetadata(false)->willReturn($cm);
$comparison->__toString()->willReturn('filterCompleteness.ratio < 100');
$cm->getAssociationMapping('completenesses')->willReturn(['targetEntity' => 'test']);
$expr->literal('100')->willReturn('100');
$expr->lt(Argument::any(), '100')->willReturn($comparison);
$this->setQueryBuilder($qb);
$qb->leftJoin('PimCatalogBundle:Locale', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
$qb->leftJoin('PimCatalogBundle:Channel', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
$qb->leftJoin('test', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
$qb->setParameter('cLocaleCode', Argument::any())->willReturn($qb);
$qb->setParameter('cScopeCode', Argument::any())->willReturn($qb);
$qb->andWhere('filterCompleteness.ratio < 100')->shouldBeCalled();
$this->addFieldFilter('completeness', '<', 100, 'en_US', 'mobile');
}
示例12: __construct
/**
* Constructor.
*
* @param Query|QueryBuilder $query
* A Doctrine ORM query or query builder.
* @param boolean $fetchJoinCollection
* Whether the query joins a collection (true by default).
* @param boolean $cache
* Use result cache (true by default).
* @param boolean $count
* Execute count query (true by default).
*/
public function __construct($query, $fetchJoinCollection = true, $cached = true, $count = true)
{
if ($count) {
$countQuery = clone $query;
$countQuery = $countQuery->select('count(e) as c')->setFirstResult(0)->setMaxResults(1)->getQuery();
$this->countQuery = $countQuery;
}
$q = $query;
if ($cached) {
$this->cache_prefix = '';
if ($query instanceof QueryBuilder) {
$q = $query->getQuery();
$entities = $query->getRootEntities();
if ($entities) {
$entity = $entities[0];
$this->cache_prefix = strtolower(substr(strrchr($entity, '\\'), 1) ?: $entity) . '-';
}
}
$q->useQueryCache(true)->useResultCache(true, 3600, $this->cache_prefix . md5($q->getDQL()));
}
parent::__construct($q, $fetchJoinCollection);
}
示例13: joinACL
/**
* Joins with the authorizations and filters the results to keep only those authorized.
*
* @param QueryBuilder $qb
* @param SecurityIdentityInterface $identity
* @param string $action
* @param string|null $entityClass Class name of the entity that is the resource in the query.
* If omitted, it will be guessed from the SELECT.
* @param string|null $entityAlias Alias of the entity that is the resource in the query.
* If omitted, it will be guessed from the SELECT.
*
* @throws \RuntimeException The query builder has no "select" part
*/
public static function joinACL(QueryBuilder $qb, SecurityIdentityInterface $identity, $action, $entityClass = null, $entityAlias = null)
{
if ($entityClass === null) {
$rootEntities = $qb->getRootEntities();
if (!isset($rootEntities[0])) {
throw new \RuntimeException('The query builder has no "select" part');
}
$entityClass = $rootEntities[0];
}
if ($entityAlias === null) {
$rootAliases = $qb->getRootAliases();
if (!isset($rootAliases[0])) {
throw new \RuntimeException('The query builder has no "select" part');
}
$entityAlias = $rootAliases[0];
}
$qb->innerJoin('MyCLabs\\ACL\\Model\\Authorization', 'authorization', 'WITH', $entityAlias . '.id = authorization.entityId');
$qb->andWhere('authorization.entityClass = :acl_entity_class');
$qb->andWhere('authorization.securityIdentity = :acl_identity');
$qb->andWhere('authorization.actions.' . $action . ' = true');
$qb->setParameter('acl_identity', $identity);
$qb->setParameter('acl_entity_class', $entityClass);
}
示例14: getClassMetadataFromJoinAlias
/**
* Gets the class metadata from a given join alias.
*
* @param string $alias
* @param QueryBuilder $queryBuilder
* @param ManagerRegistry $managerRegistry
*
* @return ClassMetadata
*/
public static function getClassMetadataFromJoinAlias(string $alias, QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry) : ClassMetadata
{
$rootEntities = $queryBuilder->getRootEntities();
$rootAliases = $queryBuilder->getRootAliases();
$joinParts = $queryBuilder->getDQLPart('join');
$aliasMap = [];
$targetAlias = $alias;
foreach ($joinParts as $rootAlias => $joins) {
$aliasMap[$rootAlias] = 'root';
foreach ($joins as $join) {
$alias = self::getJoinAlias($join);
$relationship = self::getJoinRelationship($join);
$pos = strpos($relationship, '.');
$aliasMap[$alias] = ['parentAlias' => substr($relationship, 0, $pos), 'association' => substr($relationship, $pos + 1)];
}
}
$associationStack = [];
$rootAlias = null;
while (null === $rootAlias) {
$mapping = $aliasMap[$targetAlias];
if ('root' === $mapping) {
$rootAlias = $targetAlias;
} else {
$associationStack[] = $mapping['association'];
$targetAlias = $mapping['parentAlias'];
}
}
$rootEntity = $rootEntities[array_search($rootAlias, $rootAliases)];
$rootMetadata = $managerRegistry->getManagerForClass($rootEntity)->getClassMetadata($rootEntity);
$metadata = $rootMetadata;
while (null !== ($association = array_pop($associationStack))) {
$associationClass = $metadata->getAssociationTargetClass($association);
$metadata = $managerRegistry->getManagerForClass($associationClass)->getClassMetadata($associationClass);
}
return $metadata;
}
示例15: buildWithSubQuery
/**
* Get the results from the sql after adding the
*
* @param \Victoire\Bundle\BusinessEntityPageBundle\Entity\BusinessEntityPagePattern $containerEntity
* @param QueryBuilder $itemsQueryBuilder
*
* @throws \Exception
*
* @return QueryBuilder The QB to list of objects
*/
public function buildWithSubQuery($containerEntity, QueryBuilder $itemsQueryBuilder)
{
//services
$entityManager = $this->entityManager;
//test the container entity
if ($containerEntity === null) {
throw new \Exception('The container entity parameter must not be null.');
}
//verify that the object has the query trait
$this->checkObjectHasQueryTrait($containerEntity);
//get the query of the container entity
$query = $containerEntity->getQuery();
if ($query !== '' && $query !== null) {
$subQuery = $this->entityManager->createQueryBuilder()->select('item.id')->from($itemsQueryBuilder->getRootEntities()[0], 'item');
$itemsQueryBuilder->andWhere('main_item.id IN (' . $subQuery->getQuery()->getDql() . ' ' . $query . ')');
}
$currentView = $this->currentView;
// If the current page is a BEP, we parse all its properties and inject them as query parameters
if ($currentView() && $currentView() instanceof BusinessEntityPage && null !== ($currentEntity = $currentView()->getBusinessEntity())) {
// NEW
$metadatas = $this->entityManager->getClassMetadata(get_class($currentEntity));
foreach ($metadatas->fieldMappings as $fieldName => $field) {
if (strpos($query, ":" . $fieldName) !== false) {
$itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName));
}
}
foreach ($metadatas->associationMappings as $fieldName => $field) {
if (strpos($query, ":" . $fieldName) !== false) {
$itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId());
}
}
if (strpos($query, ":currentEntity") !== false) {
$itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId());
}
}
return $itemsQueryBuilder;
}