Builds the query for the given conditions and executes it
Will return an result with 1 or 0 rows

@param array|string $conditions
@param string|null $orderBy
@return Result
553817
public function findOne($conditions, string $orderBy = null): Result
    {
        $query = $this->query()
            ->where($conditions)
            ->limit(1);

        if ($orderBy) {
            $query->orderBy($orderBy);
        }

        return $query->query();
    }
1242178
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $query = $this->createQueryBuilder($criteria, $orderBy, $limit, $offset);
        return $this->getResultFromQueryBuilder($query);
    }
681549
public function count($conditions = null, $values = [])
    {
        $res = $this->executeQuery('COUNT(' . $this->key . ') AS num', $conditions, $values)
            ->fetch();
        return (isset($res->num) ? (int)$res->num : 0);
    }
1021599
public function select($table, $where, $orderBy = null)
    {
        return $this->fieldSelect($table, "*", $where, $orderBy);
    }
283683
public function sortByArrayKeys(array $orderBy)
    {
        $result = $this->target;

        if ($orderBy) {
            $ordered = [];

            foreach ($orderBy as $key) {
                if (array_key_exists($key, $result)) {
                    $ordered[$key] = $result[$key];
                    unset($result[$key]);
                }
            }

            $result = $ordered + $result;
        }

        return $result;
    }
1061567
public function findWhere(array $conditions, $limit = null)
    {
        $result = $this->db
            ->from($this->collection)
            ->getWhere($conditions, $limit);

        if ($result->count() > 0) {
            $this->result = new DataObjects\Result($result, $this);

            if ($limit == 1) {
                return $this->result->first();
            }

            return $this->result;
        }

        return false;
    }
864341
public function getUsers($orderBy = null)
    {
        $users = $this->createQueryBuilder('o')
            ->where('o.enabled = 1');

        $this->applySorting($users, $orderBy);

        return $users
            ->getQuery()
            ->getResult();
    }
1147383
public function findOneBy(array $criteria, array $orderBy = null)
    {
        $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);

        return $persister->load($criteria, null, null, array(), null, 1, $orderBy);
    }
294206
private function orderByHasEncryptedField(array $orderBy = null)
    {
        $result = false;

        if ($orderBy && !empty($orderBy)) {
            $encryptedFields = array_keys($this->getEncryptedFields());
            $orderByFields = array_keys($orderBy);
            $commonFields = array_intersect($encryptedFields, $orderByFields);

            $result = !empty($commonFields);
        }

        return $result;
    }
817900
public function getSort($sortBy = 'create_at', $orderBy = 'desc')
    {
        $inputs = array(
            'sortBy' => $sortBy,
            'orderBy' => $orderBy
        );
        
        $rules = array(
            'sortBy'  => 'required|regex:/^[a-zA-Z0-9 _-]*$/',
            'orderBy' => 'required|regex:/^[a-zA-Z0-9 _-]*$/'
        );
        
        $validation = Validator::make($inputs, $rules);

        if ($validation->fails()) {
            return redirect($this->pageRoute)->withErrors($validation);
        }
        
        if ($orderBy != 'asc' && $orderBy != 'desc') {
            $orderBy = 'asc';
        }
        
        $models = $this->query->orderBy($sortBy, $orderBy)->paginate($this->perpage);
        
        $data = [
            'models' => $models,
            'sortBy' => $sortBy,
            'orderBy' => $orderBy
        ];
        
        // Merge if there're default data
        if (! empty($this->data)) {
            $data = array_merge($this->data, $data);
        }
        
        return view($this->pageView, $data);
    }
509144
final public function orderBy(string $orderBy, string $order = 'ASC')
    {        
       $this->query->addOrderBy($orderBy, $order);

        return $this;
    }
143320
public function findBy(array $where, array $orderBy = null, $limit = null, $offset = null)
    {
        return $this->getRepository()->findBy($where, $orderBy, $limit, $offset);
    }
1161481
public function load(array $criteria, array $orderBy = null)
    {
        $stmt = $this->getMatchCypher($criteria, $orderBy);
        $result = $this->_em->getDatabaseDriver()->run($stmt->text(), $stmt->parameters());

        if ($result->size() > 1) {
            throw new \LogicException(sprintf('Expected only 1 record, got %d', $result->size()));
        }

        $hydrator = $this->_em->getEntityHydrator($this->_className);
        $entities = $hydrator->hydrateAll($result);

        return count($entities) === 1 ? $entities[0] : null;
    }
1022544
public function findWhere(array $conditions, $limit = null)
    {
        $result = new ArrayIterator();

        $counter = 0;
        foreach ($this->storage as $row) {
            foreach ($conditions as $field => $criteria) {
                if ($row->offsetGet($field) === $criteria) {
                    $result[] = $row;
                    $counter++;
                }
            }

            if (isset($limit)) {
                if ($counter == $limit) {
                    break;
                }
            }
        }

        if ($result->count() > 0) {
            $this->result = $result;

            if ($result->count() == 1) {
                return $result->first();
            } else {
                return $this->result;
            }
        }

        return false;
    }
685996
public function select(
        $table,
        $conditions = null,
        $fields = null,
        $order = null,
        $limit = null,
        $offset = null
    )
    {
        if (is_null($fields)) {
            $fields = "*";
        }

        $queryString = "SELECT {$fields} FROM {$table} ";

        if (!is_null($conditions)) {
            $queryString .= "WHERE {$conditions} ";
        }

        if (!is_null($order)) {
            $queryString .= "ORDER BY {$order} ";
        }

        if (!is_null($limit)) {
            $queryString .= "LIMIT {$limit} ";
        }

        if (!is_null($offset) && !is_null($limit)) {
            $queryString .= "OFFSET {$offset} ";
        }


        $queryString .= ";";

        $this->query($queryString);

        return $this->countRows();
    }
701291
public function createQueryBuilder(array $criteria = null, array $orderBy = null, $limit = null, $offset = null, $class = null)
    {
        $baseQueryBuilder = $this->createBaseQueryBuilder($class);

        $queryBuilder = $this->alterBaseQueryBuilder($baseQueryBuilder);

        if (!is_null($criteria)) {
            foreach ($criteria as $key => $value) {
                $queryBuilder->andWhere($queryBuilder->getRootAliases()[0] . '.' . $key . ' = ' . $this->addNamedParameter($key, $value, $queryBuilder));
            }
        }

        if (!is_null($orderBy)) {
            foreach ($orderBy as $sort => $order) {
                $queryBuilder->addOrderBy($queryBuilder->getRootAliases()[0] . '.' . $sort, $order);
            }
        }

        if (!is_null($limit)) {
            $queryBuilder->setMaxResults($limit);
        }

        if (!is_null($offset)) {
            $queryBuilder->setFirstResult($offset);
        }

        return $queryBuilder;
    }
296203
public function findOne($where = null, $orderby = null)
  {
    $m = $this->getManager();
    $q = $m->getQueryBuilder()
      ->select($this->getTable())
      ->where($where)
      ->orderby($orderby)
      ->limit(1);

    return $this->fetchObject($m->query($q));
  }