M
M
Mariolis2017-03-25 17:08:37
MySQL
Mariolis, 2017-03-25 17:08:37

What is the error in pagination request in Symfony?

There are two functions in ProductRepository:

public function getLatestProducts($currentPage = 1, $limit = 5)
    {
        $qb = $this->createQueryBuilder('p')
            ->orderBy('p.type', 'DESC')
            ->getQuery();

        $paginator = $this->paginate($qb, $currentPage, $limit);

        return $paginator;

    }

    public function paginate($dql, $page = 1, $limit = 5)
    {
        $paginator = new Paginator($dql);

        $paginator->getQuery()
            ->setFirstResult($limit * ($page - 1)) // Offset
            ->setMaxResults($limit); // Limit

        return $paginator;
    }

When calling getLatestProducts() an error occurs:
An exception occurred while executing 'SELECT DISTINCT id_0 FROM (SELECT p0_.id AS id_0, p0_.title AS title_1, p0_.type AS type_2, p0_.status AS status_3, p0_.price AS price_4, p0_.price_sale AS price_sale_5, p0_.created AS created_6, p0_.updated AS updated_7, p0_.number_orders AS number_orders_8, p0_.quantity AS quantity_9, p0_.image_name AS image_name_10, p0_.serial AS serial_11 FROM product p0_) dctrn_result ORDER BY type_2 DESC LIMIT 10 OFFSET 0':

SQLSTATE[HY000]: General error: 3065 Expression #1 of ORDER BY clause is not in SELECT list, references column 'dctrn_result.type_2' which is not in SELECT list; this is incompatible with DISTINCT

Which is eliminated if removed from the function . What is the problem? How can I add sorting to a query so that the objects are returned in the right order?
->orderBy('p.type', 'DESC')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question