public function getMenu() { $criteria = new Criteria(); $criteria->orderBy(['hierarchy' => 'asc']); $criteria->andWhere($criteria->expr()->eq('parent', null)); return $this->adminMenuRepository->matching($criteria); }
/** * Adds filters to a query builder * * @param QueryBuilder $qb * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...] * or \Doctrine\Common\Collections\Criteria */ protected function addFilters(QueryBuilder $qb, $filters) { if ($filters) { if (is_array($filters)) { $newCriteria = new Criteria(); foreach ($filters as $fieldName => $value) { $newCriteria->andWhere(Criteria::expr()->eq($fieldName, $value)); } $filters = $newCriteria; } if ($filters instanceof Criteria) { $qb->addCriteria($filters); } } }
public function searchClients(array $criteria, array $orderBy = null, $page = 0, $pageSize = 10) { $params = new ClientSearch($criteria); $criteriaObj = new Criteria(); if ($params->getApprovedforbusiness()) { $criteriaObj->andWhere($criteriaObj->expr()->eq('approvedforbusiness', 1)); } if ($params->getClientcode() != null) { $criteriaObj->andWhere($criteriaObj->expr()->contains('clientcode', $params->getClientcode())); } if ($params->getSearchtext() != null) { $criteriaObj->andWhere($criteriaObj->expr()->orX($criteriaObj->expr()->contains('name', $params->getSearchtext()), $criteriaObj->expr()->contains('address', $params->getSearchtext()), $criteriaObj->expr()->contains('postcode', $params->getSearchtext()))); } if ($orderBy != null) { $criteriaObj->orderBy($orderBy); } return $this->clientsRepo->searchByCriteria($criteriaObj, $page, $pageSize); }
/** * Initialize mocks for Doctrine * * @param array $managersToMock List of managers to be mocked * * @return void */ protected function initDatabaseMock($managersToMock) { if (is_null($this->mockedManager)) { $test = $this; foreach ($managersToMock as $manager) { $entityName = $manager['entityName']; // EntityManager mock $entityManagerMock = new \mock\Doctrine\ORM\EntityManager(); // ClassMetadata mock $classMetadata = new \mock\Doctrine\ORM\Mapping\ClassMetadata($entityName); $entityClassName = $manager['entityClassName']; $this->calling($classMetadata)->getName = function () use($entityClassName) { return $entityClassName; }; // EntityRepository mock $entityRepositoryMock = new \mock\Doctrine\ORM\EntityRepository($entityManagerMock, $classMetadata); $this->calling($entityRepositoryMock)->find = function ($id) use($test, $entityName) { if (!empty($test->database[$entityName]) && array_key_exists($id, $test->database[$entityName])) { return clone $test->database[$entityName][$id]; } return null; }; $this->calling($entityRepositoryMock)->findBy = function ($criteria = [], $sort = null, $limit = null, $start = 0) use($test, $entityName) { $entities = new ArrayCollection($test->database[$entityName]); $crit = new Criteria(); foreach ($criteria as $field => $value) { $crit->andWhere($crit->expr()->eq($field, $value)); } if (!is_null($sort)) { $crit->orderBy($sort); } $crit->setFirstResult($start); $crit->setMaxResults($limit); return $entities->matching($crit)->map(function ($item) { return clone $item; }); }; // Overload main EntityManager functions $this->calling($entityManagerMock)->getRepository = function () use($entityRepositoryMock) { return $entityRepositoryMock; }; $this->calling($entityManagerMock)->getClassMetadata = function ($entity) use($classMetadata) { return $classMetadata; }; $this->calling($entityManagerMock)->persist = function ($entity) use($test, $entityName) { if (!$entity->getId()) { if (!empty($test->database[$entityName])) { $entity->setId(count($test->database[$entityName]) + 1); } else { $entity->setId(1); } } $test->database[$entityName][$entity->getId()] = $entity; return true; }; $this->calling($entityManagerMock)->remove = function ($entity) use($test, $entityName) { if (!$entity->getId() || empty($test->database[$entityName][$entity->getId()])) { return false; } unset($test->database[$entityName][$entity->getId()]); return true; }; $mockClass = '\\mock' . $manager['className']; $managerMock = new $mockClass($entityManagerMock, $entityName); $this->mockedManager[$manager['serviceName']] = $managerMock; } } }
/** * @param int $limit * @param int $lastId * * @return Collection|Car[] */ public function getList($limit, $lastId) { $criteria = new Criteria(); $criteria->andWhere($criteria->expr()->gt('id', $lastId)); return $this->repository->matching($criteria)->slice(0, $limit); }
/** * @param Game $game * @param string $eventType * @param int $player 1|2 * @return Criteria */ private function createFindForGameByTypeAndPlayerCriteria(Game $game, $eventType = null, $player = null) { $criteria = new Criteria(); $expr = Criteria::expr(); $criteria->where($expr->eq('game', $game)); if ($eventType !== null) { $criteria->andWhere($expr->eq('type', $eventType)); } if ($player !== null) { $criteria->andWhere($expr->eq('player', $player)); } return $criteria; }
/** * {@inheritdoc} */ public function apply(Criteria $criteria, FilterValue $value = null) { $expr = $this->createExpression($value); if (null !== $expr) { $criteria->andWhere($expr); } }
/** * Filter by select value * @param Filter\FilterSelect $filter * @return void */ public function applyFilterSelect(Filter\FilterSelect $filter) { foreach ($filter->getCondition() as $column => $value) { $expr = Criteria::expr()->eq($column, $value); $this->criteria->andWhere($expr); } }
public function getProductReviews(ProductInterface $product) : Collection { $criteria = new Criteria(); $criteria->where($criteria->expr()->eq('product', $product)); $criteria->andWhere($criteria->expr()->eq('enabled', true)); return $this->matching($criteria); }
/** * Returns the report's criteria * * @param ReportConfiguration $configuration * * @return Criteria */ protected function getCriteria(ReportConfiguration $configuration) { $criteria = new Criteria(); $criteria->where($criteria->expr()->gte('createdAt', $configuration->getStartDate())); $criteria->andWhere($criteria->expr()->lte('createdAt', $configuration->getEndDate())); return $criteria; }
public function findByTag($tagstring) { $criteria = new Criteria(); $criteria->andWhere(Criteria::expr()->contains('t.name', "%" . $tagstring . "%")); $queryBuilder = $this->_em->createQueryBuilder()->select('p')->distinct(true)->from($this->getEntityName(), "p")->leftJoin('Product\\Entity\\ProductTags', 'pt', \Doctrine\ORM\Query\Expr\Join::WITH, 'p.id = pt.idProduct')->leftJoin('Product\\Entity\\Tags', 't', \Doctrine\ORM\Query\Expr\Join::WITH, 't.id = pt.idTag')->addCriteria($criteria)->orderBy("p.creationdate", "ASC")->getQuery(); return $queryBuilder->getArrayResult(); }
private function findPreviousOrdersToday() : Collection { $today = Carbon::now()->startOfDay(); $criteria = new Criteria(); $criteria->where($criteria->expr()->gte('updatedAt', $today)); $criteria->andWhere($criteria->expr()->eq('confirmed', true)); return $this->orderRepository->matching($criteria); }
/** * Finds operations * * @param Status $status status to filter upon * @param DateTime $date optional date to filter upon * * @return OperationInterface[] */ public function findByStatusAndBeforeUpdatedAt(Status $status, DateTime $date) { $criteria = new Criteria(); $exprBuilder = new ExpressionBuilder(); $criteria->where($exprBuilder->eq('status', $status->getValue())); $criteria->andWhere($exprBuilder->lte('updatedAt', $date)); return $this->matching($criteria)->toArray(); }
/** * Add ACL checks to Criteria * * @param string $className * @param Criteria $criteria * @param string $permission * * @return Criteria */ public function applyAclToCriteria($className, Criteria $criteria, $permission) { $conditionData = $this->builder->getAclConditionData($className, $permission); if (!empty($conditionData)) { $entityField = $value = $pathExpressionType = $organizationField = $organizationValue = $ignoreOwner = null; list($entityField, $value, $pathExpressionType, $organizationField, $organizationValue, $ignoreOwner) = $conditionData; if (!is_null($organizationField) && !is_null($organizationValue)) { $criteria->andWhere(Criteria::expr()->in($organizationField, [$organizationValue])); } if (!$ignoreOwner && !empty($value)) { if (!is_array($value)) { $value = [$value]; } $criteria->andWhere(Criteria::expr()->in($entityField, $value)); } } return $criteria; }
public function testAndWhere() { $expr = new Comparison("field", "=", "value"); $criteria = new Criteria(); $criteria->where($expr); $expr = $criteria->getWhereExpression(); $criteria->andWhere($expr); $where = $criteria->getWhereExpression(); $this->assertInstanceOf('Doctrine\\Common\\Collections\\Expr\\CompositeExpression', $where); $this->assertEquals(CompositeExpression::TYPE_AND, $where->getType()); $this->assertSame(array($expr, $expr), $where->getExpressionList()); }
public function testMatchingUsesThePersisterOnlyOnce() { $foo = new stdClass(); $bar = new stdClass(); $baz = new stdClass(); $foo->val = 'foo'; $bar->val = 'bar'; $baz->val = 'baz'; $this->persister->expects($this->once())->method('loadCriteria')->with($this->criteria)->will($this->returnValue(array($foo, $bar, $baz))); $criteria = new Criteria(); $criteria->andWhere($criteria->expr()->eq('val', 'foo')); $filtered = $this->lazyCriteriaCollection->matching($criteria); $this->assertInstanceOf('Doctrine\\Common\\Collections\\Collection', $filtered); $this->assertEquals(array($foo), $filtered->toArray()); $this->assertEquals(array($foo), $this->lazyCriteriaCollection->matching($criteria)->toArray()); }
/** * @param UserSearch|null $search * @return Collections\Criteria */ private function buildCriteriaFromSearch(UserSearch $search = null) { $criteria = new Collections\Criteria(); if (!isset($search)) { return $criteria; } foreach ($search->getAsComparisonDefinitions() as $definition) { list($fieldName, $comparisonOperator, $fieldValue) = $definition; if (!isset($fieldValue)) { // ignore nulls in search form continue; } $criteria->andWhere(new Collections\Expr\Comparison($fieldName, $comparisonOperator, $fieldValue)); } return $criteria; }
protected function arrayToCriteria($filter) { if (empty($filter)) { return null; } if ($filter instanceof Criteria) { return $filter; } $criteia = new Criteria(); foreach ($filter as $key => $value) { if (is_array($value)) { $expr = $criteia->expr()->eq($key, $value); } else { $expr = $criteia->expr()->in($key, $value); } $criteia->andWhere($expr); } return $criteia; }
/** * Returns a query builder which can be used to get a list of calendar events * * @param int $calendarId * @param bool $withConnections If true events from connected calendars will be returned as well * @param array|Criteria $filters Additional filtering criteria, e.g. ['allDay' => true, ...] * or \Doctrine\Common\Collections\Criteria * * @return QueryBuilder */ public function getEventListQueryBuilder($calendarId, $withConnections = false, $filters = []) { /** @var QueryBuilder $qb */ $qb = $this->createQueryBuilder('e')->select('c.id as calendar, e.id, e.title, e.description, e.start, e.end, e.allDay, e.createdAt, e.updatedAt')->innerJoin('e.calendar', 'c'); if (is_array($filters)) { $newCriteria = new Criteria(); foreach ($filters as $fieldName => $value) { $newCriteria->andWhere(Criteria::expr()->eq($fieldName, $value)); } $filters = $newCriteria; } if ($filters) { $qb->addCriteria($filters); } if ($withConnections) { $calendarRepo = $this->getEntityManager()->getRepository('OroCalendarBundle:Calendar'); $qbConnections = $calendarRepo->createQueryBuilder('c1')->select('ac.id')->innerJoin('c1.connections', 'a')->innerJoin('a.connectedCalendar', 'ac')->where('c1.id = :id')->setParameter('id', $calendarId); $qb->andWhere($qb->expr()->in('c.id', $qbConnections->getDQL()))->setParameter('id', $calendarId); } else { $qb->andWhere('c.id = :id')->setParameter('id', $calendarId); } return $qb; }
/** {@inheritdoc} @override */ public function matching(Criteria $criteria) { if (null !== ($config = $this->getConfig()) && $config->get('multitenant.enabled')) { $criteria->andWhere($criteria->expr()->eq($config->get('multitenant.keymap'), $config->get('app.key'))); } return parent::matching($criteria); }
/** * Check existing owner in given array stack * @param array $stack * @return bool */ public function isOwnerInCollection($stack) { $criteria = new Criteria(); $criteria->andWhere($criteria->expr()->eq('organization', $this->getOrganization()))->andWhere($criteria->expr()->eq('user', $this->getUser())); return (bool) count($stack->matching($criteria)); }
/** * @ApiDoc( * section="Car", * description="Get list of wheels", * resource=true, * output={ * "class" = "Ibrows\AppBundle\Entity\Wheel[]", * "groups" = { "wheel_list" } * } * ) * * @FOSRest\Get("/cars/{car}/wheels") * * @param Car $car * @param ParamFetcher $paramFetcher * * @ParamConverter(name="car", converter="doctrine.orm", class="IbrowsAppBundle:Car") * * @FOSRest\QueryParam(name="limit", requirements="\d+", default="10", description="Limit Results") * @FOSRest\QueryParam(name="lastId", requirements="\d+", default="0", description="Last Result") * * @IbrowsAPI\View( * serializerGroups={ "wheel_list" }, * routeParams={ "car" }, * ) * * @return Wheel[] */ public function cgetAction(Car $car, ParamFetcher $paramFetcher) { $criteria = new Criteria(); $criteria->andWhere($criteria->expr()->gt('id', $paramFetcher->get('lastId'))); return $car->getWheels()->matching($criteria)->slice(0, $paramFetcher->get('limit')); }
/** * Checks the given criteria and converts them to Criteria object if needed * * @param Criteria|array|null $criteria * * @return Criteria */ public static function normalizeCriteria($criteria) { if (null === $criteria) { $criteria = new Criteria(); } elseif (is_array($criteria)) { $newCriteria = new Criteria(); foreach ($criteria as $fieldName => $value) { $newCriteria->andWhere(Criteria::expr()->eq($fieldName, $value)); } $criteria = $newCriteria; } return $criteria; }
public function getAssociatedEntities($aFieldName, ClassMetadata $aSourceMeta, $aSourceId) { $sourceEntity = $this->fetchEntity($aSourceMeta, $aSourceId); $entities = []; list($filters, $orders, $limit, $offset) = $this->getFetchEntitiesParameters(); $criteria = new Criteria(null, $orders, $offset, $limit); foreach ($filters as $f => $v) { $criteria->andWhere(Criteria::expr()->eq($f, $v)); } foreach ($sourceEntity->{$aFieldName}->matching($criteria) as $entity) { $entities[] = $entity; } $this->responseEntities($entities, $filters, $orders, $limit, $offset); }
/** * {@inheritdoc} */ public function containsKey(PersistentCollection $collection, $key) { $mapping = $collection->getMapping(); if (!isset($mapping['indexBy'])) { throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); } $persister = $this->uow->getEntityPersister($mapping['targetEntity']); // only works with single id identifier entities. Will throw an // exception in Entity Persisters if that is not the case for the // 'mappedBy' field. $criteria = new Criteria(); $criteria->andWhere(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner())); $criteria->andWhere(Criteria::expr()->eq($mapping['indexBy'], $key)); return (bool) $persister->count($criteria); }
/** * {@inheritdoc} */ public function filter(Criteria $criteria, ObjectFieldStateInterface $field, array $options) { $criteria->andWhere($criteria->expr()->andX($criteria->expr()->gt($field->name, $options["start"]), $criteria->expr()->lt($field->name, $options["end"]))); }
public function testNormalizeCriteriaArray() { $expectedCriteria = new Criteria(); $expectedCriteria->andWhere(Criteria::expr()->eq('field', 'value')); $this->assertEquals($expectedCriteria, QueryUtils::normalizeCriteria(['field' => 'value'])); }
/** * Process global search * * @param Criteria $criteria * @param RequestInterface $request */ protected function processGlobalSearch(Criteria $criteria, RequestInterface $request) { $query = $request->getGlobalSearch(); if ($query === null) { return; } $condition = call_user_func_array([Criteria::expr(), 'orX'], array_map(function ($name) use($query) { return Criteria::expr()->contains($name, $query); }, $this->schemaProvider->getSchema()->getGloballySearchableNames())); $criteria->andWhere($condition); }
/** * {@inheritdoc} */ public function filter(Criteria $criteria, ObjectFieldStateInterface $field, array $options) { $criteria->andWhere($criteria->expr()->andX($criteria->expr()->neq($field->name, ""), $criteria->expr()->isNull($field->name))); }
/** * @param Criteria $criteria * @param string $paramName * @param string $operator * @param string $value */ protected function addCriteria(Criteria $criteria, $paramName, $operator, $value) { $exprBuilder = Criteria::expr(); switch ($operator) { case '>': $expr = $exprBuilder->gt($paramName, $value); break; case '<': $expr = $exprBuilder->lt($paramName, $value); break; case '>=': $expr = $exprBuilder->gte($paramName, $value); break; case '<=': $expr = $exprBuilder->lte($paramName, $value); break; case '<>': if (is_array($value)) { $expr = $exprBuilder->notIn($paramName, $value); } else { $expr = $exprBuilder->neq($paramName, $value); } break; case '=': default: if (is_array($value)) { $expr = $exprBuilder->in($paramName, $value); } else { $expr = $exprBuilder->eq($paramName, $value); } break; } $criteria->andWhere($expr); }