Beispiel #1
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;
 }
Beispiel #2
0
 /**
  * @return self
  */
 public function save()
 {
     if (!empty($this->changedPreferences)) {
         $this->preferenceDao->getEntityManager()->flush($this->changedPreferences);
         $this->changedPreferences = [];
     }
     return $this;
 }
Beispiel #3
0
 /**
  * @param Nette\Utils\ArrayHash $vals
  *
  * @Secure\Create(allow="admin")
  * @Secure\Update(allow="admin")
  */
 public function save(Nette\Utils\ArrayHash $vals)
 {
     foreach ($vals as $key => $value) {
         if ($entity = $this->findOneBy(['key' => $key])) {
             if ($entity->value != $value) {
                 $entity->value = $value;
                 $this->dao->add($entity);
             }
         } else {
             $entity = new Entity\Setting();
             $entity->key = $key;
             $entity->value = $value;
             $this->dao->add($entity);
         }
     }
     $em = $this->dao->getEntityManager();
     $em->flush();
 }
Beispiel #4
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);
     }
 }
Beispiel #5
0
 /**
  * @return array
  */
 public function fulltextSearch($search)
 {
     $where = '';
     $ftMinWordLen = 4;
     //MySQL: ft_min_word_len
     preg_match_all("~[\\pL\\pN_]+('[\\pL\\pN_]+)*~u", stripslashes($search), $matches);
     foreach ($matches[0] as $part) {
         if (iconv_strlen($part, 'utf-8') < $ftMinWordLen) {
             $accents = ['aá', 'cč', 'dď', 'eéě', 'ií', 'nň', 'oó', 'rř', 'sš', 'tť', 'uúů', 'yý', 'zž'];
             foreach ($accents as $accent) {
                 $part = preg_replace("<[{$accent}]>iu", "[{$accent}]+", $part);
             }
             $regexp = "REGEXP '[[:<:]]" . addslashes(mb_strtoupper($part, 'UTF-8')) . "[[:>:]]'";
             $where .= " OR (title {$regexp} OR body {$regexp})";
         }
     }
     $em = $this->dao->getEntityManager();
     $rsm = new Doctrine\ORM\Query\ResultSetMapping();
     $rsm->addScalarResult('id', 'id');
     $rsm->addScalarResult('title_score', 'title_score');
     $rsm->addScalarResult('body_score', 'body_score');
     $sql = "SELECT id, 5 * MATCH(title) AGAINST (?) AS title_score, MATCH(body) AGAINST (?) AS body_score\n\t\t\t\tFROM posts WHERE MATCH(title, body) AGAINST(? IN BOOLEAN MODE){$where}\n\t\t\t\tORDER BY 5 * MATCH(title) AGAINST (?) + MATCH(body) AGAINST (?) DESC";
     $query = $em->createNativeQuery($sql, $rsm);
     $query->setParameters([$search, $search, $search, $search, $search]);
     $result = $query->getScalarResult();
     $ids = array_map('current', $result);
     //FIXME:WARNING: temporary ugly hack because WHERE id IN (79, 10, 45, 54, 62) doesn't keep order
     $tmp = [];
     foreach ($ids as $key => $value) {
         $relevance = $result[$key]['title_score'];
         $article = $this->findOneBy(['id' => $value, 'publish_date <=' => new \DateTime()]);
         if (empty($article)) {
             continue;
         }
         $tmp[$key . '#' . $relevance] = $article;
     }
     return $tmp;
     //return $this->findBy(array('id' => $ids));
 }