Exemple #1
0
 /**
  * @param \Doctrine\DBAL\Exception\UniqueConstraintViolationException $e
  * @param \Kdyby\Doctrine\EntityDao $dao
  * @param \Carrooi\Doctrine\Entities\BaseEntity $entity
  * @return static
  */
 public static function createFromDuplicateEntryException(UniqueConstraintViolationException $e, EntityDao $dao, BaseEntity $entity)
 {
     $match = Strings::match($e->getMessage(), '/DETAIL:\\s+Key\\s\\(([a-z_]+)\\)/');
     $column = $dao->getClassMetadata()->getColumnName($match[1]);
     $value = $dao->getClassMetadata()->getFieldValue($entity, $match[1]);
     return new static($e, $entity, $column, $value);
 }
 private static function getPairsAdvanced(EntityDao $dao, array $options)
 {
     $qb = $dao->createQueryBuilder('e')->select("e")->resetDQLPart('from')->from($dao->getClassName(), 'e', 'e.' . $options['key'])->autoJoinOrderBy((array) $options['orderBy']);
     foreach ($options['criteria'] as $key => $value) {
         if (is_numeric($key) && is_callable($value)) {
             $value($qb, 'e');
         } else {
             $qb->whereCriteria([$key => $value]);
         }
     }
     $query = $qb->getQuery();
     if (is_string($options['value'])) {
         $parts = explode('.', $options['value']);
         $callback = function ($value) use($parts) {
             foreach ($parts as $part) {
                 $value = $value->{$part};
             }
             return $value;
         };
     } else {
         $callback = $options['value'];
     }
     $result = $query->getResult();
     return array_map($callback, $result);
 }
Exemple #3
0
 /**
  * @param \Carrooi\Doctrine\Entities\BaseEntity $entity
  * @return \Carrooi\Doctrine\Entities\BaseEntity
  */
 public function save(BaseEntity $entity)
 {
     try {
         $this->dao->add($entity);
         $this->dao->getEntityManager()->flush();
     } catch (UniqueConstraintViolationException $e) {
         $e = DuplicateEntryException::createFromDuplicateEntryException($e, $this->getDao(), $entity);
         $this->processDuplicateEntryException($e);
     }
     return $entity;
 }
 public function deleteComment($c, ICommentable $e)
 {
     try {
         $wpDb = $this->pageDao->find($e->getId());
         if ($wpDb !== null) {
             $coll = $wpDb->getComments();
             $id = $this->getMixId($c);
             $comment = $coll->filter(function ($e) use($id) {
                 return $e->getId() == $id;
             })->first();
             $index = $coll->indexOf($comment);
             if (!is_numeric($index)) {
                 return;
             }
             $coll->remove($index);
             $this->entityManager->merge($wpDb);
             $this->entityManager->flush($wpDb);
             $this->commentService->deleteComment($id);
             $this->invalidateEntityCache($wpDb);
         }
     } catch (Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #5
0
 public function updateRule(AclRule $arule)
 {
     if ($arule === null) {
         throw new Exceptions\NullPointerException("Argument AclRule cannot be null");
     }
     try {
         $this->entityManager->beginTransaction();
         $dbRule = $this->aclRuleDao->find($arule->getId());
         if ($dbRule !== null) {
             $dbRule->fromArray($arule->toArray());
             $this->roleTypeHandle($dbRule);
             $this->entityManager->merge($dbRule);
             $this->entityManager->flush();
         }
         $this->entityManager->commit();
         $this->invalidateEntityCache($dbRule);
         $this->onUpdate($dbRule);
     } catch (DuplicateEntryException $e) {
         $this->entityManager->rollback();
         $this->logWarning($e);
         throw new Exceptions\DuplicateEntryException($e->getMessage(), $e->getCode(), $e->getPrevious());
     } catch (\Exception $e) {
         $this->entityManager->rollback();
         $this->logError($e);
         throw new Exceptions\DataErrorException($e->getMessage(), $e->getCode(), $e->getPrevious());
     }
 }
Exemple #6
0
 public function getComments()
 {
     try {
         return $this->commentDao->findAll();
     } catch (\Exception $ex) {
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #7
0
 public function getCurrentSeason()
 {
     try {
         return $this->seasonDao->createQueryBuilder("s")->where("s.current = true")->getQuery()->getSingleResult();
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #8
0
 /**
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function getDql()
 {
     $qb = $this->commentDao->createQueryBuilder('a');
     if ($this->recipient) {
         $qb = $qb->andWhere('a.recipient = :recipient')->setParameter('recipient', $this->recipient);
     } else {
         $qb = $qb->andWhere('a.recipient IS NULL');
     }
     return $qb;
 }
Exemple #9
0
 /**
  * @param string $name
  * @param string $email
  * @param string $password
  * @param Role[]|string[] $roles
  */
 public function create($name, $email, $password, $roles = [])
 {
     $user = new User();
     $user->name = $name;
     $user->email = $email;
     $this->passwordStrategy->setPassword($user, $password);
     $roles = (array) $roles;
     foreach ($roles as $role) {
         if (is_string($role)) {
             if (!($role = $this->rolesRepository->findBy(['name' => $role]))) {
                 throw new \RuntimeException("Unknown role '{$role}'.");
             }
         } elseif (!$role instanceof Role) {
             throw new \UnexpectedValueException("Role must be string or instance of Rixxi\\User\\Entities\\Role");
         }
         $user->roles->add($role);
     }
     $this->repository->save($user);
     return $user;
 }
Exemple #10
0
 /**
  * @return int|null|string
  */
 public function findOneByKey($key)
 {
     $result = $this->dao->findOneBy(['key' => $key]);
     if ($result) {
         $result = $result->value;
         return is_numeric($result) ? $result + 0 : $result;
         // int | double | string
     } else {
         return NULL;
     }
 }
Exemple #11
0
 /**
  * @param BaseEntity $entity
  * @throws ModelException
  */
 public function save(BaseEntity $entity)
 {
     $em = $this->dao->getEntityManager();
     $em->beginTransaction();
     try {
         $em->flush();
         $em->commit();
     } catch (\Exception $e) {
         $em->rollback();
         throw new ModelException($e->getMessage(), $e->getCode(), $e);
     }
 }
Exemple #12
0
 public function getSeasonTaxSG(Season $s, SportGroup $sg)
 {
     try {
         $res = $this->seasonTaxDao->createQueryBuilder("st")->where("st.season = :season")->setParameter("season", $s)->andWhere("st.sportGroup = :group")->setParameter("group", $sg)->getQuery()->getSingleResult();
         return $res;
     } catch (NoResultException $ex) {
         $this->logWarning($ex->getMessage());
         throw new Exceptions\NoResultException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
 /**
  * @param Brabijan\SeoComponents\Entity\Route $route
  */
 public function delete(Brabijan\SeoComponents\Entity\Route $route)
 {
     $target = $route->target;
     $isRouteOneWay = $route->oneWay;
     $this->routeDao->delete($route);
     if ($isRouteOneWay === FALSE) {
         $target = new Brabijan\SeoComponents\Router\Target($target->targetPresenter, $target->targetAction, $target->targetId);
         $currentRoute = $this->findCurrentRouteByTarget($target);
         if ($currentRoute) {
             $this->cleanTargetCache($target);
             $currentRoute->oneWay = FALSE;
             $this->routeDao->save($currentRoute);
         }
     }
 }
Exemple #14
0
 public function getNews()
 {
     try {
         $cache = $this->getEntityCache();
         $data = $cache->load(self::COLLECTION_NEWS);
         if ($data === null) {
             $data = $this->articleDao->createQueryBuilder("a")->orderBy("a.created", "DESC")->setMaxResults($this->getDefaultRssLimit())->getQuery()->getResult();
             $opts = [Cache::TAGS => [self::ENTITY_COLLECTION, self::SELECT_COLLECTION, self::COLLECTION_NEWS], Cache::SLIDING => true];
             $cache->save(self::COLLECTION_NEWS, $data, $opts);
         }
         return $data;
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #15
0
 /**
  * @param array $names
  * @return PreferenceEntity[]
  */
 private function loadPreferences(array $names)
 {
     $tmp = array_filter($names, function ($name) {
         return !array_key_exists($name, $this->preferences);
     });
     if (!empty($tmp)) {
         $result = $this->preferenceDao->findAssoc(['name' => $tmp], 'name');
         foreach ($tmp as $name) {
             $this->preferences[$name] = isset($result[$name]) ? $result[$name] : NULL;
         }
     }
     $preferences = [];
     foreach ($names as $name) {
         $preferences[$name] = $this->preferences[$name];
     }
     return $preferences;
 }
Exemple #16
0
 public function changeWebProfile(User $u)
 {
     try {
         $wp = $u->getWebProfile();
         $user = $this->userDao->find($u->getId());
         if ($wp->getPicture() instanceof \Nette\Http\FileUpload && $wp->getPicture()->isOk()) {
             $oldImgId = $wp->provideOldImgId();
             $this->imageService->removeResource($oldImgId);
             $identifier = $this->imageService->storeNetteFile($wp->getPicture());
             $wp->setPicture($identifier);
         }
         $this->entityManager->flush();
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #17
0
 private function markAs($id, User $user, $as)
 {
     try {
         $db = $this->paymentDao->find($id);
         if ($db !== null) {
             $db->setStatus($as);
             $db->setEditor($user);
             $this->paymentEditorTypeHandle($db);
             $this->entityManager->merge($db);
             $this->entityManager->flush();
             $this->invalidateEntityCache($db);
         }
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
 public function getUsersApplication(User $u, Season $s)
 {
     try {
         $id = "{$this->getEntityClassName()}/{$u->getId()}-{$s->getId()}";
         $cache = $this->getEntityCache();
         $data = $cache->load($id);
         if ($data === null) {
             $data = $this->seasonApplicationDao->createQueryBuilder("a")->where("a.owner = :owner")->setParameter("owner", $u->getId())->andWhere("a.season = :season")->setParameter("season", $s->getId())->getQuery()->getSingleResult();
             $opts = [Cache::TAGS => [self::ENTITY_COLLECTION, $id]];
             $cache->save($id, $data, $opts);
         }
         return $data;
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #19
0
 /**
  * @param FileUpload $file
  *
  * @return ArrayHash
  */
 private function getFileData(FileUpload $file)
 {
     $name = $file->getSanitizedName();
     $extension = '';
     if (Strings::contains($name, '.')) {
         $fragments = explode('.', $name);
         array_shift($fragments);
         $extension = implode('.', $fragments);
     }
     $pairs = $this->storageDao->findPairs([], 'name', [], 'id');
     $data = new ArrayHash();
     while (true) {
         $name = Random::generate(10, '0-9A-Za-z');
         if (!in_array($name, $pairs)) {
             $data->name = $name;
             $data->extension = $extension;
             break;
         }
     }
     return $data;
 }
Exemple #20
0
 public function getAllSportGroups($root = null, $active = null)
 {
     if ($active !== null && !is_bool($active)) {
         throw new Exceptions\InvalidArgumentException("Argument active has to be type of bool, '{$active}' given");
     }
     try {
         if ($root !== null) {
             $qb = $this->entityManager->createQueryBuilder();
             $qb = $qb->select("g")->from("App\\Model\\Entities\\SportGroup", "g")->where("g.parent = :parent");
             if ($active !== null) {
                 $qb = $qb->andWhere("g.activity = :act")->setParameter("act", $active);
             }
             $qb = $qb->orderBy("ASC", "g.priority, g.name")->setParameter("parent", $root);
             return $qb->getQuery()->getResult();
         }
         return $this->groupDao->findAll();
     } catch (\Exceptions $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
 public function updateEntry(MotivationEntry $e)
 {
     try {
         $db = $this->entryDao->find($e->getId());
         if ($db !== null) {
             $db->fromArray($e->toArray());
             $db->setUpdated(new DateTime());
             $this->entryEditorTypeHandle($db);
             $this->entryOwnerTypeHandle($db);
             $this->entrySeasonTypeHandle($db);
             $this->entityManager->merge($db);
             $this->entityManager->flush();
             $this->invalidateEntityCache($db);
             $this->onUpdate($db);
         }
     } catch (DuplicateEntryException $ex) {
         $this->logWarning($ex->getMessage());
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #22
0
 public function getPositionsWithinGroup(SportGroup $g, $useCache = true)
 {
     try {
         $qb = $this->positionDao->createQueryBuilder();
         $qb->select("p")->from("App\\Model\\Entities\\Position", "p")->where("p.group = :group")->setParameter("group", $g);
         $q = $qb->getQuery();
         if (!$useCache) {
             return $q->getResult();
         }
         $id = User::getClassName() . "in" . SportGroup::getClassName() . "-" . $g->getId();
         $cache = $this->getEntityCache();
         $data = $cache->load($id);
         if ($data == null) {
             $data = $q->getResult();
             $opts = [Cache::TAGS => [self::ENTITY_COLLECTION, self::STRANGER_COLLECTION, $id], Cache::SLIDING => true];
             $cache->save($id, $data, $opts);
         }
         return $data;
     } catch (\Exception $e) {
         $this->logError($e);
         throw new Exceptions\DataErrorException($e->getMessage(), $e->getCode(), $e->getPrevious());
     }
 }
Exemple #23
0
 public function updateRole(Role $r)
 {
     if ($r === null) {
         throw new Exceptions\NullPointerException("Argument Role cannot be null");
     }
     try {
         $dbRole = $this->roleDao->find($r->getId());
         if ($dbRole !== null) {
             $dbRole->fromArray($r->toArray());
             $dbRole->setParents($this->roleParentsCollSetup($r));
             //$dbRole->setAdded(new \Nette\Utils\DateTime()); // this might cause hierarchy problems
             $this->entityManager->merge($dbRole);
             $this->entityManager->flush();
             $this->invalidateEntityCache($dbRole);
             $this->onUpdate(clone $dbRole);
         }
     } catch (DuplicateEntryException $e) {
         throw new Exceptions\DuplicateEntryException($e->getMessage(), $e->getCode(), $e->getPrevious());
     } catch (\Exception $e) {
         $this->logError($e);
         throw new Exceptions\DataErrorException($e->getMessage(), $e->getCode(), $e->getPrevious());
     }
 }
Exemple #24
0
 public function updatePartner(Partner $p)
 {
     try {
         $db = $this->partnerDao->find($p->getId());
         if ($db !== null) {
             $this->partnerPictureHandle($p, $db);
             $db->fromArray($p->toArray());
             $db->setUpdated(new DateTime());
             $this->editorTypeHandle($db);
             $this->referrerTypeHandle($db);
             $this->entityManager->merge($db);
             $this->entityManager->flush();
             $this->invalidateEntityCache($db);
             $this->onUpdate($db);
         }
     } catch (\Kdyby\Doctrine\DuplicateEntryException $ex) {
         $this->logWarning($ex);
         throw new Exceptions\DuplicateEntryException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     } catch (\Exception $ex) {
         $this->logError($ex->getMessage());
         throw new Exceptions\DataErrorException($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
     }
 }
Exemple #25
0
 /**
  * @return mixed
  */
 public function countBy(array $criteria = [])
 {
     return $this->dao->countBy($criteria);
 }
Exemple #26
0
 public function delete($entity, $relations = NULL, $flush = Kdyby\Persistence\ObjectDao::FLUSH)
 {
     $this->dao->delete($entity, $relations, $flush);
 }
Exemple #27
0
 /**
  * @param array $criteria
  * @param array $orderBy
  * @param null $limit
  * @param null $offset
  * @return \ArrayIterator
  */
 public function findForApi(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 {
     $query = $this->dao->createQueryBuilder('p')->whereCriteria($criteria)->autoJoinOrderBy((array) $orderBy)->leftJoin('p.tags', 'tt')->addSelect('tt')->getQuery();
     $resultSet = new Kdyby\Doctrine\ResultSet($query);
     return $resultSet->applyPaging($offset, $limit)->getIterator(Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
 }
Exemple #28
0
 public function getUserEventsDatasource(User $u)
 {
     $model = new Doctrine($this->participationDao->createQueryBuilder()->select("ep, ev.title AS title, ev.takePlaceSince as takePlaceSince, ev.takePlaceTill AS takePlaceTill, ev.eventType AS eventType")->add("from", EventParticipation::getClassName() . " ep LEFT JOIN ep.event ev")->where("ep.owner = :owner")->setParameter("owner", $u->getId()));
     return $model;
 }
 /**
  * @param \Brabijan\SeoComponents\Entity\Meta $meta
  */
 public function save(\Brabijan\SeoComponents\Entity\Meta $meta)
 {
     $this->metaDao->save($meta);
 }
Exemple #30
0
 public function getByNameOrEmail($nameOrEmail)
 {
     return $this->repository->fetchOne(new FindUserByNameOrEmailQuery($nameOrEmail));
 }