/**
  * @param Fields $fields
  *
  * @return array
  */
 protected function getColumns(Fields $fields)
 {
     $newFields = [];
     foreach ($this->mapping->map() as $objectProperty => $tableColumn) {
         if (in_array($objectProperty, $fields->get())) {
             $newFields[$objectProperty] = $tableColumn;
         }
     }
     return $newFields;
 }
 /**
  * Returns all instances of the type meeting $distinctFields values.
  *
  * @param Fields      $distinctFields
  * @param Filter|null $filter
  * @param Sort|null   $sort
  *
  * @return array
  */
 public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
 {
     $model = self::$instance;
     $query = $model->query();
     $columns = count($fields = $distinctFields->get()) > 0 ? $fields : ['*'];
     if ($filter) {
         EloquentFilter::filter($query, $filter);
     }
     if ($sort) {
         EloquentSorter::sort($query, $sort);
     }
     return $query->getQuery()->distinct()->get($columns);
 }
Ejemplo n.º 3
0
 /**
  * @param Fields $distinctFields
  * @param        $results
  *
  * @return array
  *
  * @throws \Exception
  */
 protected function resultsWithDistinctFieldsOnly(Fields $distinctFields, $results) : array
 {
     $newResults = [];
     $valueHash = [];
     foreach ($results as $result) {
         $distinctValues = [];
         foreach ($distinctFields->get() as $field) {
             $distinctValues[$field] = PropertyValue::get($result, $field);
         }
         $hash = md5(serialize($distinctValues));
         if (false === in_array($hash, $valueHash)) {
             $valueHash[] = $hash;
             $newResults[] = $result;
         }
     }
     return $newResults;
 }
 /**
  * {@inheritdoc}
  */
 public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) : array
 {
     $hashFilter = $filter ? serialize($filter->filters()) : '';
     $hashSort = $sort ? serialize($sort->orders()) : '';
     $hashFields = $distinctFields ? serialize($distinctFields->get()) : '';
     $key = $this->cacheNamespaceFindByDistinct . md5($hashFilter . $hashSort . $hashFields);
     $cachedItem = $this->cache->getItem($key);
     if ($cachedItem->isHit() && null !== ($result = $cachedItem->get())) {
         return (array) $result;
     }
     $result = $this->repository->findByDistinct($distinctFields, $filter, $sort);
     $this->saveToCache($cachedItem, $result);
     return (array) $result;
 }