本文整理汇总了PHP中Doctrine\ORM\QueryBuilder::getEntityManager方法的典型用法代码示例。如果您正苦于以下问题:PHP QueryBuilder::getEntityManager方法的具体用法?PHP QueryBuilder::getEntityManager怎么用?PHP QueryBuilder::getEntityManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\QueryBuilder
的用法示例。
在下文中一共展示了QueryBuilder::getEntityManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: array
/**
* @param Repository $EntityRepository
* @param array $Filter array( 'ColumnName' => 'Value', ... )
*/
function __construct(Repository $EntityRepository, $Filter = array())
{
$this->QueryBuilder = $EntityRepository->createQueryBuilder(self::QUERY_BUILDER_ALIAS);
$this->ColumnNames = $this->QueryBuilder->getEntityManager()->getClassMetadata($EntityRepository->getClassName())->getFieldNames();
$this->Request = $this->getRequest()->getParameterArray();
$this->setFilter($Filter);
$this->setSearch();
$this->setOrderBy();
}
示例2: 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;
}
示例3: getFixedQueryBuilder
/**
* This method alters the query to return a clean set of object with a working
* set of Object
*
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
* @return void
*/
private function getFixedQueryBuilder(QueryBuilder $queryBuilder)
{
$queryBuilderId = clone $queryBuilder;
// step 1 : retrieve the targeted class
$from = $queryBuilderId->getDQLPart('from');
$class = $from[0]->getFrom();
// step 2 : retrieve the column id
$idName = current($queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class)->getIdentifierFieldNames());
// step 3 : retrieve the different subjects id
$select = sprintf('%s.%s', $queryBuilderId->getRootAlias(), $idName);
$queryBuilderId->resetDQLPart('select');
$queryBuilderId->add('select', 'DISTINCT ' . $select);
$results = $queryBuilderId->getQuery()->execute(array(), Query::HYDRATE_ARRAY);
$idx = array();
$connection = $queryBuilder->getEntityManager()->getConnection();
foreach ($results as $id) {
$idx[] = $connection->quote($id[$idName]);
}
// step 4 : alter the query to match the targeted ids
if (count($idx) > 0) {
$queryBuilder->andWhere(sprintf('%s IN (%s)', $select, implode(',', $idx)));
$queryBuilder->setMaxResults(null);
$queryBuilder->setFirstResult(null);
}
return $queryBuilder;
}
示例4: resolveEntityClass
/**
* Gets the full class name if the given join expression represents an entity name in form "bundle:class"
*
* @param string $joinExpr
*
* @return string
*/
protected function resolveEntityClass($joinExpr)
{
$parts = explode(':', $joinExpr);
if (count($parts) !== 2) {
return $joinExpr;
}
return $this->originalQueryBuilder->getEntityManager()->getConfiguration()->getEntityNamespace($parts[0]) . '\\' . $parts[1];
}
示例5: resetBuilder
protected function resetBuilder()
{
$rootEntities = $this->qb->getRootEntities();
$this->qb = $this->qb->getEntityManager()->getRepository($rootEntities[0])->createQueryBuilder('x');
$this->joinAliases = [];
$this->parametersMap = [];
$this->orderBy = [];
}
示例6: initIdFieldName
/**
* Initialize the column id of the targeted class.
*
* @return string
*/
protected function initIdFieldName()
{
/** @var $from \Doctrine\ORM\Query\Expr\From */
$from = current($this->originalQb->getDQLPart('from'));
$class = $from->getFrom();
$idNames = $this->originalQb->getEntityManager()->getMetadataFactory()->getMetadataFor($class)->getIdentifierFieldNames();
$this->idFieldName = current($idNames);
}
示例7: createNumRowsSubQuery
private function createNumRowsSubQuery()
{
$parts = $this->query->getDQLParts();
$from = $parts['from'][0];
$where = $parts['where'];
$entityManager = $this->query->getEntityManager();
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('COUNT(DISTINCT countAlias.id)')->from($from->getFrom(), 'countAlias');
$aliasCount = 1;
$aliases = array($from->getAlias() => 'countAlias');
foreach ($parts['join'] as $joinSource => $joins) {
if (!array_key_exists($joinSource, $aliases)) {
$aliases[$joinSource] = 'countAlias' . $aliasCount;
++$aliasCount;
}
foreach ($joins as $join) {
if (!array_key_exists($join->getAlias(), $aliases)) {
$aliases[$join->getAlias()] = 'countAlias' . $aliasCount;
++$aliasCount;
}
preg_match('#[a-zA-Z_]*\\.+#', $join->getJoin(), $matches);
$joinCondition = preg_replace('#[a-zA-Z_]*\\.+#', $aliases[substr($matches[0], 0, -1)] . '.', $join->getJoin());
switch ($join->getJoinType()) {
case \Doctrine\ORM\Query\Expr\Join::INNER_JOIN:
$queryBuilder->innerJoin($joinCondition, $aliases[$join->getAlias()]);
break;
case \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN:
$queryBuilder->leftJoin($joinCondition, $aliases[$join->getAlias()]);
break;
default:
throw new \Exception('Unknow join type');
break;
}
}
}
if ($where) {
$whereCondition = $where->__toString();
preg_match_all('#([a-zA-Z_]*)\\.+#', $whereCondition, $matches);
foreach ($matches[1] as $alias) {
$whereCondition = preg_replace('#' . $alias . '\\.+#', $aliases[$alias] . '.', $whereCondition);
}
$queryBuilder->where($whereCondition);
}
return $queryBuilder;
}
示例8: applyToCollection
/**
* {@inheritdoc}
*/
public function applyToCollection(ResourceInterface $resource, QueryBuilder $queryBuilder)
{
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resource->getEntityClass());
$identifiers = $classMetaData->getIdentifier();
if (null !== $this->order && 1 === count($identifiers)) {
$identifier = $identifiers[0];
$queryBuilder->addOrderBy('o.' . $identifier, $this->order);
}
}
示例9: 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;
}
示例10: applyToCollection
/**
* {@inheritdoc}
*/
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
$identifiers = $classMetaData->getIdentifier();
if (null !== $this->order) {
foreach ($identifiers as $identifier) {
$queryBuilder->addOrderBy('o.' . $identifier, $this->order);
}
}
}
示例11: initializeDataStructure
protected function initializeDataStructure($tableName, $primaryKey)
{
$dataStructure = new Structures\DataStructure($tableName, $primaryKey);
$this->setDataStructure($dataStructure);
$entityManager = $this->queryBuilder->getEntityManager();
$classMetadata = $entityManager->getClassMetadata($tableName);
Helpers::setStructureFromColumns($dataStructure, $this->determineFromColumns($classMetadata));
foreach ($classMetadata->getAssociationNames() as $associationName) {
$targetClass = $classMetadata->getAssociationTargetClass($associationName);
$targetMetadata = $entityManager->getClassMetadata($targetClass);
$associations = $classMetadata->getAssociationsByTargetClass($targetClass);
$primaryColumns = $targetMetadata->getIdentifierColumnNames();
foreach ($associations as $association) {
$dataStructure->getOrCreateTableStructure($targetClass, reset($primaryColumns));
$associationsType = $association['type'];
if ($associationsType === $classMetadata::ONE_TO_ONE) {
if ($association['sourceEntity'] === $tableName) {
$referencedColumn = reset($association['targetToSourceKeyColumns']);
}
if (!isset($referencedColumn)) {
throw new InvalidStateException(sprintf('Can not find referenced column name for field %s.', $associationName));
}
$dataStructure->addOneToOne($associationName, $targetClass, $referencedColumn);
} elseif ($associationsType === $classMetadata::MANY_TO_ONE) {
if ($association['sourceEntity'] === $tableName) {
$referencedColumn = reset($association['targetToSourceKeyColumns']);
}
if (!isset($referencedColumn)) {
throw new InvalidStateException(sprintf('Can not find referenced column name for field %s.', $associationName));
}
$dataStructure->addManyToOne($associationName, $targetClass, $referencedColumn);
} elseif ($associationsType === $classMetadata::ONE_TO_MANY) {
foreach ($targetMetadata->getAssociationMappings() as $joinedAssociation) {
if ($joinedAssociation['inversedBy'] === $associationName) {
$referencedColumn = reset($joinedAssociation['targetToSourceKeyColumns']);
}
}
if (!isset($referencedColumn)) {
throw new InvalidStateException(sprintf('Can not find referenced column name for field %s. Is `inversedBy` set in property annotation?', $associationName));
}
$dataStructure->addOneToMany($associationName, $targetClass, $referencedColumn);
} elseif ($associationsType === $classMetadata::MANY_TO_MANY) {
if (!isset($association['joinTable'])) {
throw new InvalidStateException(sprintf('Can not find joinColumn settings for field %s. Is `joinColumn` set in property annotation?', $associationName));
}
$joinTable = $association['joinTable'];
if (isset($joinTable['joinColumns']) && $joinTable['joinColumns']) {
$joinColumn = reset($joinTable['joinColumns']);
$inverseJoinColumn = reset($joinTable['inverseJoinColumns']);
$dataStructure->addManyToMany($associationName, $targetClass, $inverseJoinColumn['name'], $joinTable['name'], $joinColumn['name']);
}
}
}
}
}
示例12: normalize
/**
* @inheritdoc
*/
public function normalize(Query $query, QueryBuilder $queryBuilder, $hydratorClass = null)
{
/*
* Add custom hydrator
*/
$emConfig = $queryBuilder->getEntityManager()->getConfiguration();
$hydrator = new \ReflectionClass($hydratorClass);
$hydratorName = $hydrator->getShortName();
$emConfig->addCustomHydrationMode($hydratorName, $hydratorClass);
return $query->getResult($hydratorName);
}
示例13: 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));
}
}
示例14: joinRelations
/**
* Left joins relations to eager load.
*
* @param ResourceInterface $resource
* @param QueryBuilder $queryBuilder
*/
private function joinRelations(ResourceInterface $resource, QueryBuilder $queryBuilder)
{
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resource->getEntityClass());
foreach ($classMetaData->getAssociationNames() as $i => $association) {
$mapping = $classMetaData->associationMappings[$association];
if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
$queryBuilder->leftJoin('o.' . $association, 'a' . $i);
$queryBuilder->addSelect('a' . $i);
}
}
}
示例15: applyAcl
/**
* Apply ACL restriction to query builder.
*
* @param QueryBuilder $queryBuilder
* @param string $tableAlias
*/
public function applyAcl(QueryBuilder $queryBuilder, $tableAlias)
{
$taggableEntities = $this->getTaggableEntities($queryBuilder->getEntityManager());
$allowedEntities = $this->getAllowedEntities($taggableEntities);
if (count($allowedEntities) != count($taggableEntities)) {
if ($allowedEntities) {
$queryBuilder->andWhere($tableAlias . '.entityName IN(:allowedEntities)')->setParameter('allowedEntities', $allowedEntities);
} else {
// Do not show any result if all entities are prohibited
$queryBuilder->andWhere('1 = 0');
}
}
}