findOneBy() public method

Finds a single object by a set of criteria.
public findOneBy ( array $criteria ) : object | null
$criteria array The criteria.
return object | null The object.
コード例 #1
14
 /**
  * @param $data
  * @return AbstractDefaultTypedAddress
  */
 protected function createAddress($data)
 {
     /** @var Country $country */
     $country = $this->countryRepository->findOneBy(['iso2Code' => $data['country']]);
     if (!$country) {
         throw new \RuntimeException('Can\'t find country with ISO ' . $data['country']);
     }
     /** @var Region $region */
     $region = $this->regionRepository->findOneBy(['country' => $country, 'code' => $data['state']]);
     if (!$region) {
         throw new \RuntimeException(printf('Can\'t find region with country ISO %s and code %s', $data['country'], $data['state']));
     }
     $types = [];
     $typesFromData = explode(',', $data['types']);
     foreach ($typesFromData as $type) {
         $types[] = $this->addressTypeRepository->find($type);
     }
     $defaultTypes = [];
     $defaultTypesFromData = explode(',', $data['defaultTypes']);
     foreach ($defaultTypesFromData as $defaultType) {
         $defaultTypes[] = $this->addressTypeRepository->find($defaultType);
     }
     $address = $this->getNewAddressEntity();
     $address->setTypes(new ArrayCollection($types));
     $address->setDefaults(new ArrayCollection($defaultTypes))->setPrimary(true)->setLabel('Primary address')->setCountry($country)->setStreet($data['street'])->setCity($data['city'])->setRegion($region)->setPostalCode($data['zipCode']);
     return $address;
 }
コード例 #2
0
ファイル: CategoryService.php プロジェクト: shophp/shophp
 private function checkDuplicity(Category $category)
 {
     $duplicate = $this->repository->findOneBy(['path' => $category->getPath()]);
     if ($duplicate !== null) {
         throw new EntityDuplicateException(sprintf('Category with path %s already exists.', $category->getPath()));
     }
 }
コード例 #3
0
ファイル: UserService.php プロジェクト: shophp/shophp
 private function checkDuplicity(User $user)
 {
     $duplicate = $this->repository->findOneBy(['email' => $user->getEmail()]);
     if ($duplicate !== null) {
         throw new EntityDuplicateException(sprintf('User with e-mail %s already exists.', $user->getEmail()));
     }
 }
コード例 #4
0
ファイル: RegionConverter.php プロジェクト: dairdr/crm
 /**
  * Try to get from local property if exist or load from database afterwards
  *
  * @param string $code
  *
  * @return Region|Null
  */
 protected function tryGetMRByCode($code)
 {
     if (!isset($this->MRIdentityMap[$code]) && !array_key_exists($code, $this->MRIdentityMap)) {
         $this->MRIdentityMap[$code] = $this->repository->findOneBy(['combinedCode' => $code]);
     }
     return $this->MRIdentityMap[$code];
 }
コード例 #5
0
    function it_converts_node_to_street_entry_with_updating_existing_one(ObjectManager $om, ObjectRepository $or, Street $street)
    {
        $xml = <<<EOT
<row>
    <col name="WOJ">02</col>
    <col name="POW">23</col>
    <col name="GMI">09</col>
    <col name="RODZ_GMI">2</col>
    <col name="SYM">0884849</col>
    <col name="SYM_UL">10268</col>
    <col name="CECHA">ul.</col>
    <col name="NAZWA_1">Księżycowa </col>
    <col name="NAZWA_2"/>
    <col name="STAN_NA">2013-10-10</col>
</row>
EOT;
        $place = new Place(884849);
        $place->setName('City');
        $or->findOneBy(array('id' => '0884849'))->shouldBeCalled()->willReturn($place);
        $or->findOneBy(array('id' => '10268', 'place' => $place))->shouldBeCalled()->willReturn($street);
        $street->setName('Księżycowa')->shouldBeCalled()->willReturn($street);
        $street->setAdditionalName('')->shouldBeCalled()->willReturn($street);
        $street->setType('ul.')->shouldBeCalled()->willReturn($street);
        $this->beConstructedWith(new \SimpleXMLElement($xml), $om);
        $this->convertToEntity()->shouldBeLike($street->getWrappedObject());
    }
コード例 #6
0
 /**
  * @param $username
  * @return \AppBundle\Entity\User
  * @throws UserNotFoundException
  */
 public function getUserByUsername($username)
 {
     $user = $this->userRepository->findOneBy(array('username' => $username));
     if ($user === null) {
         throw new UserNotFoundException();
     }
     return $user;
 }
 function it_searches_book_by_isbn_number(BookInterface $book, ObjectRepository $doctrineRepository)
 {
     $isbn = new Isbn('978-1-56619-909-4');
     $doctrineRepository->findOneBy(array('isbn.number' => $isbn))->willReturn($book);
     $this->searchByIsbn($isbn)->shouldBeLike(SearchResults::fromArrayOfBooks(array($book->getWrappedObject())));
     $doctrineRepository->findOneBy(array('isbn.number' => $isbn))->willReturn(null);
     $this->searchByIsbn($isbn)->shouldBeLike(SearchResults::asEmpty());
 }
コード例 #8
0
 /**
  * {@inheritdoc}
  *
  * @throws \RuntimeException if the identifier is not set
  */
 public function findModuleByIdentity($identity)
 {
     $field = $this->getModularIdentifier();
     if (null == $field) {
         throw new \RuntimeException('The module manager is missing a modular identifier.');
     }
     return $this->repository->findOneBy([$field => $identity]);
 }
コード例 #9
0
 /**
  * This is used to return a from to rate and is used in the cron section
  * @param $fromCurrency
  * @param $toCurrency
  * @return null|object
  */
 public function findFromToRate($fromCurrency, $toCurrency)
 {
     //Check if the rate exists
     $exRateObject = $this->exRateRepository->findOneBy(array('fromCurrency' => $fromCurrency, 'toCurrency' => $toCurrency));
     if (!$exRateObject instanceof ExchangeRate) {
         return null;
     }
     return $exRateObject;
 }
コード例 #10
0
 /**
  * @param $categoryName
  * @return Category
  */
 public function getProductsByCategoryName($categoryName)
 {
     try {
         $category = $this->repository->findOneBy(['name' => $categoryName, 'isActive' => 1]);
     } catch (\Exception $e) {
         $category = new Category(self::CATEGORY_NOT_FOUND);
     }
     return $category;
 }
コード例 #11
0
 /**
  * {@inheritdoc}
  */
 public function transform($value)
 {
     if (!$value) {
         return null;
     }
     if (null === ($entity = $this->repository->findOneBy(array($this->identifier => $value)))) {
         throw new TransformationFailedException(sprintf('Object "%s" with identifier "%s"="%s" does not exist.', $this->repository->getClassName(), $this->identifier, $value));
     }
     return $entity;
 }
コード例 #12
0
 public function release($name)
 {
     $nameWithPrefix = $this->getNameWithPrefix($name);
     $lock = $this->repository->findOneBy(['name' => $nameWithPrefix]);
     if ($lock) {
         $this->objectManager->remove($lock);
         $this->objectManager->flush();
         return true;
     }
     return false;
 }
コード例 #13
0
 public function createUserIdentity($user)
 {
     list($className, $identifier) = $this->extractUserIdentityFields($user);
     if (isset($this->userCache[$className][$identifier])) {
         return $this->userCache[$className][$identifier];
     }
     if (null !== ($this->userCache[$className][$identifier] = $this->userRepository->findOneBy(array('class' => $className, 'identifier' => $identifier)))) {
         return $this->userCache[$className][$identifier];
     }
     $userClass = $this->userRepository->getClassName();
     return $this->userCache[$className][$identifier] = new $userClass($className, $identifier);
 }
コード例 #14
0
 /**
  * @param string $name
  * @return object
  */
 private function findByName($name)
 {
     $sequence = $this->repository->findOneBy(array('name' => $name));
     if (!$sequence) {
         $sequence = new $this->class();
         $sequence->setName($name);
         $sequence->setCurrentValue(0);
         $this->objectManager->persist($sequence);
         $this->objectManager->flush();
     }
     return $sequence;
 }
コード例 #15
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$value instanceof ProductInterface) {
         throw new UnexpectedTypeException($value, ProductInterface::class);
     }
     $product = $value;
     $accessor = PropertyAccess::createPropertyAccessor();
     $criteria = array($constraint->property => $accessor->getValue($product, $constraint->property));
     $conflictualProduct = $this->repository->findOneBy($criteria);
     if (null !== $conflictualProduct && $conflictualProduct != $product) {
         $this->context->addViolationAt($constraint->property, $constraint->message, array('%property%' => $constraint->property));
     }
 }
コード例 #16
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$value instanceof VariantInterface) {
         throw new UnexpectedTypeException($value, 'Sylius\\Component\\Variation\\Model\\VariantInterface');
     }
     $variant = $value;
     $accessor = PropertyAccess::createPropertyAccessor();
     $criteria = array($constraint->property => $accessor->getValue($variant, $constraint->property));
     $conflictualVariant = $this->variantRepository->findOneBy($criteria);
     if (null !== $conflictualVariant && $conflictualVariant !== $variant) {
         $this->context->addViolationAt($constraint->property, $constraint->message, array('%property%' => $constraint->property));
     }
 }
コード例 #17
0
ファイル: Authenticator.php プロジェクト: instante/doctrine
 /**
  * Performs an authentication
  * @param  array
  * @return User
  * @throws \Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->userRepository->findOneBy([$this->nameColumn => $username]);
     if ($user === NULL) {
         throw new AuthenticationException("User '{$username}' not found.", self::IDENTITY_NOT_FOUND);
     }
     if (!$user->isActive()) {
         throw new AuthenticationException("User '{$username}' is inactive.", self::NOT_APPROVED);
     }
     if (!$user->checkPassword($password)) {
         throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
     }
     return $user;
 }
コード例 #18
0
 /**
  * @param $value
  * @param IValidationData $data
  *
  * @return bool
  */
 public function check($value, IValidationData $data = null)
 {
     if (is_scalar($value)) {
         $alreadyExists = false;
         if ($data instanceof IValidationData) {
             $controlValue = $data->get($this->controlProperty);
             if ($controlValue !== null) {
                 $alreadyExists = $this->repository->findOneBy([$this->column => $value, $this->controlColumn => $controlValue]) !== null;
             }
         }
         $duplicate = $this->repository->findOneBy([$this->column => $value]);
         return $alreadyExists === true || $duplicate === null;
     }
     return false;
 }
コード例 #19
0
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($code)
 {
     if (!$code) {
         return null;
     }
     if (!($coupon = $this->couponRepository->findOneBy(['code' => $code]))) {
         $this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_INVALID, new GenericEvent());
         return null;
     }
     if (!$coupon->isValid()) {
         $this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_NOT_ELIGIBLE, new GenericEvent());
         return null;
     }
     return $coupon;
 }
コード例 #20
0
 /**
  * @param array $criteria
  *
  * @return object
  */
 protected function findUser(array $criteria)
 {
     if (null === $this->repository) {
         $this->repository = $this->em->getRepository($this->class);
     }
     return $this->repository->findOneBy($criteria);
 }
コード例 #21
0
ファイル: LoadRegions.php プロジェクト: dairdr/crm
 /**
  * @param array $regionData
  *
  * @return null|Region
  */
 protected function getRegion(array $regionData)
 {
     if (strpos($regionData['code'], $regionData['country_id'] . BAPRegion::SEPARATOR) === 0) {
         $combinedCode = $regionData['code'];
     } else {
         $combinedCode = BAPRegion::getRegionCombinedCode($regionData['country_id'], $regionData['code']);
     }
     /** @var $region Region */
     $region = $this->regionRepository->findOneBy(array('combinedCode' => $combinedCode));
     if (!$region) {
         $region = new Region($combinedCode);
         $region->setCode($regionData['code'])->setRegionId($regionData['region_id'])->setCombinedCode($combinedCode)->setCountryCode($regionData['country_id']);
     }
     $region->setName($regionData['default_name']);
     return $region;
 }
コード例 #22
0
 /**
  * Find comment.
  *
  * @param string $commentId   Comment id
  * @param string $authorToken Author token
  *
  * @return CommentInterface Comment
  *
  * @throws EntityNotFoundException Comment not found
  */
 private function findComment($commentId, $authorToken)
 {
     $comment = $this->commentRepository->findOneBy(['id' => $commentId, 'authorToken' => $authorToken]);
     if (!$comment instanceof CommentInterface) {
         throw new EntityNotFoundException('Comment not found');
     }
     return $comment;
 }
コード例 #23
0
 /**
  * @param ObjectRepository $repository
  * @param string           $referenceDataName
  * @param string           $code
  *
  * @throws \LogicException
  *
  * @return int
  */
 protected function resolveOne(ObjectRepository $repository, $referenceDataName, $code)
 {
     //TODO: do not hydrate them, use a scalar result
     $referenceData = $repository->findOneBy(['code' => $code]);
     if (null === $referenceData) {
         throw new \LogicException(sprintf('No reference data "%s" with code "%s" has been found', $referenceDataName, $code));
     }
     return $referenceData->getId();
 }
コード例 #24
0
ファイル: ShipmentService.php プロジェクト: shophp/shophp
 private function checkDuplicity(ShipmentOption $shipment)
 {
     if ($shipment instanceof ShipmentTransportCompany) {
         $duplicate = $this->transportCompanyRepository->findOneBy(['name' => $shipment->getName()]);
         if ($duplicate !== null) {
             throw new EntityDuplicateException(sprintf('Shipment company with name %s already exists.', $shipment->getName()));
         }
     }
 }
コード例 #25
0
ファイル: ResponseFactory.php プロジェクト: Volyz/barometre
 /**
  * @param Response $response
  * @param array    $specialityList
  */
 protected function addSpeciality(Response $response, array $specialityList)
 {
     foreach ($specialityList as $speciality) {
         $speciality = $this->specialityRepository->findOneBy(array('name' => trim($speciality)));
         if (!$speciality instanceof Speciality) {
             continue;
         }
         $response->addSpeciality($speciality);
     }
 }
コード例 #26
0
 function it_adds_or_removes_provinces_on_pre_submit(FormFactoryInterface $formFactory, ObjectRepository $countryRepository, FormEvent $event, FormInterface $form, FormInterface $provinceForm, CountryInterface $country)
 {
     $event->getForm()->willReturn($form);
     $event->getData()->willReturn(['countryCode' => 'FR']);
     $countryRepository->findOneBy(['code' => 'FR'])->willReturn($country);
     $country->hasProvinces()->willReturn(true);
     $formFactory->createNamed('provinceCode', 'sylius_province_code_choice', null, Argument::withKey('country'))->willReturn($provinceForm);
     $form->add($provinceForm)->shouldBeCalled();
     $this->preSubmit($event);
 }
コード例 #27
0
 /**
  * {@inheritDoc}
  */
 public function isValid($value)
 {
     $value = $this->cleanSearchValue($value);
     $match = $this->objectRepository->findOneBy($value);
     if (is_object($match)) {
         return true;
     }
     $this->error(self::ERROR_NO_OBJECT_FOUND, $value);
     return false;
 }
コード例 #28
0
 /**
  * Delete a translation of content.
  *
  * @param string $locale
  * @param $id
  */
 public function deleteTranslation($locale, $id)
 {
     if ($locale === 'en') {
         $content = $this->content->findOneBy(array('id' => $id));
     } else {
         $content = $this->translations->findOneBy(array('foreignKey' => $id, 'locale' => $locale));
     }
     if ($content instanceof ContentTranslation || $content instanceof Content) {
         $this->manager->remove($content);
         $this->manager->flush();
     }
 }
コード例 #29
0
 /**
  * {@inheritDoc}
  */
 public function authenticate()
 {
     $this->authenticateSetup();
     $identity = $this->objectRepository->findOneBy(array($this->identityProperty => $this->identityValue));
     if (!$identity) {
         $this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
         $this->authenticationResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
         return $this->authenticateCreateAuthResult();
     }
     $authResult = $this->authenticateValidateIdentity($identity);
     return $authResult;
 }
コード例 #30
0
ファイル: RoleListener.php プロジェクト: nmallare/platform
 /**
  * Update role field.
  *
  * @param AbstractRole $role
  * @param ObjectRepository $repository
  * @return bool
  */
 protected function updateRole(AbstractRole $role, ObjectRepository $repository)
 {
     if ($role->getRole()) {
         return true;
     }
     $roleValue = $role->generateUniqueRole();
     if ($repository->findOneBy(['role' => $roleValue])) {
         return false;
     }
     $role->setRole($roleValue, false);
     return true;
 }