findBy() public method

Optionally sorting and limiting details can be passed. An implementation may throw an UnexpectedValueException if certain values of the sorting or limiting details are not supported.
public findBy ( array $criteria, array $orderBy = null, integer | null $limit = null, integer | null $offset = null ) : array
$criteria array
$orderBy array
$limit integer | null
$offset integer | null
return array The objects.
Example #1
0
 public function findAll()
 {
     if (false === $this->authorizationService->isGranted('user.admin')) {
         throw new UnauthorizedException('Insufficient Permissions');
     }
     return $this->objectRepository->findBy([], ['email' => 'ASC']);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function getSupportedMethods(ShippingSubjectInterface $subject)
 {
     $methods = [];
     foreach ($this->shippingMethodRepository->findBy(['enabled' => true]) as $shippingMethod) {
         if ($this->eligibilityChecker->isEligible($subject, $shippingMethod)) {
             $methods[] = $shippingMethod;
         }
     }
     return $methods;
 }
Example #3
0
 /**
  * @return array
  * @throws UnauthorizedException
  */
 public function findAll()
 {
     if ($this->authorizationService->isGranted('recipe.admin')) {
         return $this->objectRepository->findBy([], ['dateUpdated' => 'DESC']);
     }
     if ($this->authorizationService->isGranted('recipe.manage')) {
         return $this->objectRepository->findBy(['author' => $this->authorizationService->getIdentity()], ['dateUpdated' => 'DESC']);
     }
     throw new UnauthorizedException('Insufficient Permission');
 }
 /**
  * @param GenericEvent $event
  */
 public function onArchetypeUpdate(GenericEvent $event)
 {
     $archetype = $event->getSubject();
     if (!$archetype instanceof ArchetypeInterface) {
         throw new UnexpectedTypeException($archetype, ArchetypeInterface::class);
     }
     $products = $this->productRepository->findBy(array('archetype' => $archetype));
     foreach ($products as $product) {
         $this->builder->build($product);
         $this->productManager->persist($product);
     }
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function fillBackorders(StockableInterface $stockable)
 {
     $onHand = $stockable->getOnHand();
     if ($onHand <= 0) {
         return;
     }
     $units = $this->repository->findBy(array('stockable' => $stockable, 'inventoryState' => InventoryUnitInterface::STATE_BACKORDERED), array('createdAt' => 'ASC'));
     foreach ($units as $unit) {
         $unit->setInventoryState(InventoryUnitInterface::STATE_SOLD);
         if (--$onHand === 0) {
             break;
         }
     }
     $stockable->setOnHand($onHand);
 }
Example #6
0
 /**
  * Gets all zones
  *
  * @param string|null $scope
  *
  * @return array $zones
  */
 protected function getZones($scope = null)
 {
     if (null === $scope) {
         return $this->repository->findAll();
     }
     return $this->repository->findBy(array('scope' => $scope));
 }
 function it_should_index_entity(ManagerRegistry $registry, EntityManager $em, ObjectRepository $repo)
 {
     $repo->findBy([], ['id' => 'DESC'], 1, 2)->willReturn([1, 2, 3]);
     $em->getRepository('foo')->willReturn($repo);
     $registry->getManager(null)->willReturn($em);
     $this->setRegistry($registry);
     $this->index(1, 2, 'id', 'DESC')->shouldBe([1, 2, 3]);
 }
 /**
  * Get current cart automatic coupons.
  *
  * @param CartInterface $cart Cart
  *
  * @returns CouponInterface[] Array of non applied coupons
  */
 private function getNonAppliedAutomaticCoupons(CartInterface $cart)
 {
     $automaticCoupons = $this->couponRepository->findBy(['enforcement' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC]);
     $loadedAutomaticCoupons = $this->cartCouponManager->getCoupons($cart);
     return array_udiff($automaticCoupons, $loadedAutomaticCoupons, function (CouponInterface $a, CouponInterface $b) {
         return $a->getId() != $b->getId();
     });
 }
 function it_does_reverse_transform_identifiers_to_array_of_entities(ObjectRepository $repository, FakeEntity $entityOne, FakeEntity $entityTwo)
 {
     $value = array(1, 2);
     $entityOne->getId()->willReturn(1);
     $entityTwo->getId()->willReturn(2);
     $repository->findBy(array('id' => $value))->shouldBeCalled()->willReturn(array($entityOne, $entityTwo));
     $this->reverseTransform($value)->shouldReturn(array($entityOne, $entityTwo));
 }
 /**
  * Method subscribed to PreCartLoad event
  *
  * Iterate over all automatic Coupons and check if they apply.
  * If any applies, it will be added to the Cart
  *
  * @param CartOnLoadEvent $event Event
  *
  * @return null
  */
 public function tryAutomaticCoupons(CartOnLoadEvent $event)
 {
     $cart = $event->getCart();
     if ($cart->getCartLines()->isEmpty()) {
         return null;
     }
     /**
      * @var CouponInterface[] $automaticCoupons
      */
     $automaticCoupons = $this->couponRepository->findBy(['enforcement' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC]);
     foreach ($automaticCoupons as $coupon) {
         try {
             $this->cartCouponManager->addCoupon($cart, $coupon);
         } catch (AbstractCouponException $e) {
             // Silently tries next coupon on controlled exception
         }
     }
 }
 function it_returns_all_methods_eligible_for_given_subject(ObjectRepository $methodRepository, ShippingMethodEligibilityCheckerInterface $eligibilityChecker, ShippingSubjectInterface $subject, ShippingMethodInterface $method1, ShippingMethodInterface $method2, ShippingMethodInterface $method3)
 {
     $methods = [$method1, $method2, $method3];
     $methodRepository->findBy(['enabled' => true])->shouldBeCalled()->willReturn($methods);
     $eligibilityChecker->isEligible($subject, $method1)->shouldBeCalled()->willReturn(true);
     $eligibilityChecker->isEligible($subject, $method2)->shouldBeCalled()->willReturn(true);
     $eligibilityChecker->isEligible($subject, $method3)->shouldBeCalled()->willReturn(false);
     $this->getSupportedMethods($subject)->shouldReturn([$method1, $method2]);
 }
 function it_partially_fills_backordered_units_and_updates_stock_accordingly(StockableInterface $stockable, InventoryUnitInterface $inventoryUnit1, InventoryUnitInterface $inventoryUnit2, ObjectRepository $repository)
 {
     $stockable->getOnHand()->shouldBeCalled()->willReturn(5);
     $stockable->setOnHand(3)->shouldBeCalled();
     $inventoryUnit1->setInventoryState(InventoryUnitInterface::STATE_SOLD)->shouldBeCalled();
     $inventoryUnit2->setInventoryState(InventoryUnitInterface::STATE_SOLD)->shouldBeCalled();
     $repository->findBy(['stockable' => $stockable, 'inventoryState' => InventoryUnitInterface::STATE_BACKORDERED], ['createdAt' => 'ASC'])->willReturn([$inventoryUnit1, $inventoryUnit2]);
     $this->fillBackorders($stockable);
 }
 function it_schedules_owning_document_for_update_when_setting_element_by_key_in_the_collection(MongoDBODMUnitOfWork $uow, DocumentStub $document, ObjectRepository $repository, ClassMetadata $classMetadata, EntityStub $entity4, EntityStub $entity8, EntityStub $entity15, EntityStub $newEntity)
 {
     $classMetadata->getIdentifier()->willReturn(['id']);
     $repository->findBy(['id' => [4, 8, 15]])->willReturn([$entity4, $entity8, $entity15]);
     $uow->getDocumentState($document)->willReturn(MongoDBODMUnitOfWork::STATE_MANAGED);
     $uow->isScheduledForUpdate($document)->willReturn(false);
     $uow->scheduleForUpdate($document)->shouldBeCalled();
     $this->setOwner($document);
     $this->set(2, $newEntity);
 }
 /**
  * {@inheritDoc}
  */
 public function getRoles(array $roleNames)
 {
     $key = implode($roleNames);
     if (isset($this->roleCache[$key])) {
         return $this->roleCache[$key];
     }
     $roles = $this->objectRepository->findBy([$this->roleNameProperty => $roleNames]);
     // We allow more roles to be loaded than asked (although this should not happen because
     // role name should have a UNIQUE constraint in database... but just in case ;))
     if (count($roles) >= count($roleNames)) {
         $this->roleCache[$key] = $roles;
         return $roles;
     }
     // We have roles that were asked but couldn't be found in database... problem!
     foreach ($roles as &$role) {
         $role = $role->getName();
     }
     throw new RoleNotFoundException(sprintf('Some roles were asked but could not be loaded from database: %s', implode(', ', array_diff($roleNames, $roles))));
 }
 function it_updates_products_with_newer_attributes_added_to_their_archetypes(GenericEvent $event, ArchetypeInterface $archetype, ArchetypeBuilderInterface $builder, ObjectRepository $productRepository, ObjectManager $productManager, ProductInterface $productA, ProductInterface $productB)
 {
     $event->getSubject()->willReturn($archetype);
     $productRepository->findBy(array('archetype' => $archetype))->willReturn(array($productA, $productB));
     $builder->build($productA)->shouldBeCalled();
     $builder->build($productB)->shouldBeCalled();
     $productManager->persist($productA)->shouldBeCalled();
     $productManager->persist($productB)->shouldBeCalled();
     $this->onArchetypeUpdate($event);
 }
 /**
  * Here we read rules from DB and put them into an a form that BjyAuthorize's Controller.php understands
  *
  * @param string $type
  *
  * @return array
  */
 public function getRules($type = self::TYPE_RESOURCE)
 {
     //read from object store a set of (role, controller, action)
     $result = $this->objectRepository->findBy(['type' => $type]);
     //transform to object BjyAuthorize will understand
     $rules = array();
     foreach ($result as $key => $rule) {
         $role = $rule->getRole()->getRoleId();
         $controller = $rule->getResource();
         $action = $rule->getPrivileges();
         if (!$action) {
             $rules[$controller]['roles'][] = $role;
             $rules[$controller]['controller'] = array($controller);
         } else {
             $rules[$controller . ':' . $action]['roles'][] = $role;
             $rules[$controller . ':' . $action]['controller'] = array($controller);
             $rules[$controller . ':' . $action]['action'] = explode(',', $action);
         }
     }
     return array_values($rules);
 }
 /**
  * {@inheritDoc}
  */
 public function getRouteCollectionForRequest(Request $request)
 {
     $url = $request->getPathInfo();
     $collection = new RouteCollection();
     /** @var $connection \Doctrine\DBAL\Connection */
     $connection = $this->objectManager->getConnection();
     try {
         $connection->connect();
     } catch (\Exception $e) {
         return $collection;
     }
     if (!$connection->isConnected()) {
         return $collection;
     }
     $searchUrl = substr($url, -1) != '/' ? $url . '/' : $url;
     $params = array('path' => $searchUrl);
     try {
         $contentRoutes = $this->repository->findBy($params);
     } catch (\Doctrine\DBAL\DBALException $e) {
         return $collection;
     }
     if (empty($contentRoutes)) {
         return $collection;
     }
     $filterByLocale = function (ContentRouteInterface $var) use($request) {
         if ($request) {
             return $var->getLocale() == $request->getLocale();
         } else {
             return true;
         }
     };
     $tempContentRoutes = array_filter($contentRoutes, $filterByLocale);
     if (empty($tempContentRoutes)) {
         $tempContentRoutes = $contentRoutes;
     }
     foreach ($tempContentRoutes as $key => $contentRoute) {
         $viewStatus = $request ? $request->getSession()->get('_viewStatus', VersionableInterface::STATUS_PUBLISHED) : VersionableInterface::STATUS_PUBLISHED;
         $test = new \ReflectionClass($contentRoute->getClassType());
         if ($viewStatus == VersionableInterface::STATUS_DRAFT && $test->implementsInterface(ResourceVersionInterface::class)) {
             continue;
         } elseif ($viewStatus == VersionableInterface::STATUS_PUBLISHED && $test->implementsInterface(VersionableInterface::class)) {
             continue;
         }
         /** @var \Networking\InitCmsBundle\Model\ContentRouteInterface $contentRoute */
         $content = $this->getRouteContent($contentRoute);
         $collection->add(sprintf('%s/%s', $contentRoute->getLocale(), $searchUrl), static::generateRoute($contentRoute, $url, $content));
     }
     return $collection;
 }
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     if (null === $value) {
         return null;
     }
     if ($this->multiple) {
         if (is_string($value) && null !== $this->delimiter) {
             $value = explode($this->delimiter, $value);
         }
         if (!is_array($value)) {
             throw new UnexpectedTypeException($value, 'array');
         }
         return $this->repository->findBy([$this->identifierProperty => $value]);
     }
     return $this->repository->findOneBy([$this->identifierProperty => $value]);
 }
 /**
  * {@inheritDoc}
  */
 public function findContentRoutesBy($url)
 {
     $collection = new RouteCollection();
     /** @var $connection \Doctrine\DBAL\Connection */
     $connection = $this->objectManager->getConnection();
     try {
         $connection->connect();
     } catch (\Exception $e) {
         return $collection;
     }
     if (!$connection->isConnected()) {
         return $collection;
     }
     $searchUrl = substr($url, -1) != '/' ? $url . '/' : $url;
     $params = array('path' => $searchUrl);
     try {
         $contentRoutes = $this->repository->findBy($params);
     } catch (\Doctrine\DBAL\DBALException $e) {
         return $collection;
     }
     if (empty($contentRoutes)) {
         return $collection;
     }
     $tempContentRoutes = array_filter($contentRoutes, array($this, 'filterByLocale'));
     if (empty($tempContentRoutes)) {
         $tempContentRoutes = $contentRoutes;
     }
     foreach ($tempContentRoutes as $key => $contentRoute) {
         $viewStatus = $this->request ? $this->request->getSession()->get('_viewStatus', VersionableInterface::STATUS_PUBLISHED) : VersionableInterface::STATUS_PUBLISHED;
         $test = new \ReflectionClass($contentRoute->getClassType());
         if ($viewStatus == VersionableInterface::STATUS_DRAFT && $test->implementsInterface('Networking\\InitCmsBundle\\Doctrine\\Extensions\\Versionable\\ResourceVersionInterface')) {
             continue;
         } elseif ($viewStatus == VersionableInterface::STATUS_PUBLISHED && $test->implementsInterface('Networking\\InitCmsBundle\\Doctrine\\Extensions\\Versionable\\VersionableInterface')) {
             continue;
         }
         /** @var \Networking\InitCmsBundle\Model\ContentRouteInterface $contentRoute */
         $content = $this->getRouteContent($contentRoute);
         $contentRoute->setContent($content);
         $contentRoute->setPath($url);
         $collection->add(self::ROUTE_GENERATE_DUMMY_NAME . preg_replace('/[^a-z0-9A-Z_.]/', '_', $key), $contentRoute);
     }
     return $collection;
 }
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     if (null === $value) {
         return;
     }
     if ($this->multiple) {
         if (is_string($value) && null !== $this->delimiter) {
             $value = explode($this->delimiter, $value);
         }
         if (!is_array($value)) {
             throw new UnexpectedTypeException($value, 'array');
         }
         if (method_exists($this->repository, 'findByIds')) {
             return $this->repository->findByIds($value);
         } else {
             return $this->repository->findBy(['id' => $value]);
         }
     }
     return $this->repository->find($value);
 }
 /**
  * @inheritDoc
  *
  * This will look for already persisted entries only.
  */
 public function searchAces(array $grantees = null, array $targets = null, array $permissions = null)
 {
     $criteria = array();
     if (null !== $grantees) {
         $grantees = array_map(array($this, 'extractSecurityIdentity'), $grantees);
         // Filter out non persisted non-anonymous grantee
         $grantees = array_filter($grantees, function (SecurityIdentity $grantee) {
             return null !== $grantee->getId() || SecurityIdentity::KIND_ANONYMOUS === $grantee->getKind();
         });
         $grantees = array_unique(array_map(function (SecurityIdentity $grantee) {
             return $grantee->getId();
         }, $grantees), true);
         if (0 === count($grantees)) {
             return array();
         }
         $criteria['grantee'] = $this->generateCriteriaValue($grantees);
     }
     if (null !== $targets) {
         $targets = array_map(array($this, 'extractTargetIdentity'), $targets);
         $targets = array_map(function (TargetIdentity $target) {
             return $target->getId();
         });
         // Filter out non persisted targets
         $targets = array_unique(array_filter($targets));
         if (0 === count($targets)) {
             return array();
         }
         $criteria['target'] = $this->generateCriteriaValue($targets);
     }
     if (null !== $permissions) {
         if (0 === count($permissions)) {
             return array();
         }
         $criteria['permission'] = $this->generateCriteriaValue($permissions);
     }
     return $this->repository->findBy($criteria);
 }
Example #22
0
 /**
  * get all active products
  */
 public function getAllActiveProducts()
 {
     return $this->repository->findBy(['isActive' => 1]);
 }
 /**
  * {@inheritDoc}
  */
 public function findByUser(UserInterface $user)
 {
     return $this->repository->findBy(['user' => $user]);
 }
 /**
  * {@inheritDoc}
  */
 public function findSchedules($limit = null, $offset = null)
 {
     return $this->repository->findBy(array(), array(), $limit, $offset);
 }
Example #25
0
 /**
  * @param array $criteria
  *
  * @return ShippingMethodInterface[]
  */
 protected function getMethods(array $criteria = [])
 {
     return $this->repository->findBy($criteria);
 }
Example #26
0
 /**
  * {@inheritDoc}
  */
 public function findByChannel($channel)
 {
     return $this->repository->findBy(array('channel' => $channel));
 }
Example #27
0
 /**
  * Finds objects by a set of criteria.
  *
  * Optionally sorting and limiting details can be passed. An implementation may throw
  * an UnexpectedValueException if certain values of the sorting or limiting details are
  * not supported.
  *
  * @param array      $criteria
  * @param array|null $orderBy
  * @param int|null   $limit
  * @param int|null   $offset
  *
  * @return array Set of fetched objects
  *
  * @throws UnexpectedValueException
  */
 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 {
     return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
 }
 /**
  * Find by the associated user id
  * @param $user_id
  * @return object
  */
 public function findByUserId($user_id)
 {
     return $this->clientRepository->findBy(array('user' => $user_id));
 }
Example #29
0
 /**
  * @param string $path
  * @return Product[]
  */
 public function getByPath($path)
 {
     return $this->repository->findBy(['path' => $path]);
 }
Example #30
-1
 /**
  * Insert new users
  *
  * @param Collection $users
  */
 private function insertUsers(Collection $users)
 {
     set_time_limit(60);
     $this->manager->getConnection()->getConfiguration()->setSQLLogger(null);
     // Get existan user from DB
     $existantUsers = $this->repository->findBy(['email' => $this->extractEmails($users)->toArray()]);
     // Create an array of emails
     $existantEmails = array_map(function ($user) {
         return $user->getEmail();
     }, $existantUsers);
     unset($existantUsers);
     // Get not existant users ready to import
     $nonExistantUsers = $users->filter(function ($user) use($existantEmails) {
         return !in_array($user->getEmail(), $existantEmails);
     });
     unset($existantEmails);
     foreach ($nonExistantUsers as $user) {
         $user->addRole('ROLE_USER');
         $this->manager->persist($user);
         $this->manager->flush();
         $this->manager->clear();
         $this->manager->detach($user);
         gc_enable();
         gc_collect_cycles();
     }
 }