I
I
Igor2019-06-22 16:16:18
Doctrine ORM
Igor, 2019-06-22 16:16:18

How to get quantity in Doctrine orm?

How to get quantity in Doctrine orm?
Based on a previously created DQL
Or another way, the main thing is that it would be cost-effective.

public function search()
    {
        $q = $this->request->mixed('q', '');
        $offset = $this->request->mixed('offset', 0);
        $count = $this->request->mixed('count', 10);
        $category_id = (int)$this->request->mixed('category_id', 0);
        $sort = $this->request->mixed('sort', "ASC");

        $dig = 'dig';

        $fields = [
            "$dig.name",
            "$dig.image",
            "$dig.postCode",
            "$dig.phone",
            "$dig.latitude",
            "$dig.longitude",
            "$dig.home",
            "$dig.site",
            "$dig.schedule",
            "$dig.category",
        ];

        $qb = $this->em->getRepository(Digest::class)
            ->createQueryBuilder($dig);

        // join
        if ($category_id > 0)
            $qb = $qb->innerJoin("$dig.category", "cat", Expr\Join::WITH, 'cat.parent = :category_id')
                ->setParameter('category_id', $category_id);


       // where
        $qb = $qb->where("$dig.name like :name")
            ->orWhere("$dig.home like :home")
            ->setParameter("name", "%$q%")
            ->setParameter("home", "%$q%");

        // sort
        $qb  =  $qb->orderBy("$dig.id", $sort)
            ->groupBy("$dig.id");


        // count

        /** @var $qb QueryBuilder $total */
      //$dql = $qb->select("Count($dig.name)")->getDQL();

      $total = 0; // Сюда я хочу записать количество, входящее в условие запроса 
        
        // далее я выставляю лимиты, офсеты

        // limit, offset
        $qb = $qb->setMaxResults($count)
            ->setFirstResult($offset);

        /** @var QueryBuilder $qb */
//        die($qb->getDQL());

        $query = $qb->getQuery();
        $items = $query->getArrayResult();

        $data = new ResponseSchema();
        $data->setCount(count($total));
        $data->setItems($items);
        $json = $this->serializer->serialize($data, 'json', ['attributes' => [
            'count',
            'items' => [
                'name',
                'image',
                'postCode',
                'phone',
                'latitude',
                'longitude',
                'home',
                'site',
                'schedule',
            ]
        ]]);
        $this->response->jsonSend($json);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-06-22
@IgorPI

1 METHOD:
you need to transfer the sampling logic to the DigRepository repository in a certain method
, in the same place in the repository, make a method to get the number
, all these methods will work with a certain common method that returns QueryBuilder.
I recommend starting with this method
Make some Criteria object that would collect the desired QueryBuilder and give the desired answer (the data itself or their number)
You need another query with almost the same set for the QueryBuilder, just query $qb->select('count($dig.id)')
AND at the end $qb->getQuery()->getSingleScalarResult()
That's why I suggested moving the code above to where it belongs and making it more general so that you can do a search and ask for the number of records
Economically - do 2 queries this way and that, but the code will become simpler and it will be more convenient to reuse it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question