Jump to content

Module with Grid (pagination, column sort) / Moderne Controller


Recommended Posts

Hello everyone, I am creating a module with a grid. I use prestashop 1.7.8. I managed to generate my grid with data and javascript extensions. Now I want to manage column sorting and pagination. I use the following example: Prestashop example modules

Here’s my code and the error
 

<?php

namespace Antcompanyinfos\Core\Grid\Query;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use PrestaShop\PrestaShop\Core\Grid\Query\AbstractDoctrineQueryBuilder;
use PrestaShop\PrestaShop\Core\Grid\Query\DoctrineSearchCriteriaApplicatorInterface;
use PrestaShop\PrestaShop\Core\Grid\Search\SearchCriteriaInterface;

class ActivityQueryBuilder extends AbstractDoctrineQueryBuilder
{
    /**
     * @var DoctrineSearchCriteriaApplicatorInterface
     */
    private $searchCriteriaApplicator;

    /**
     * @var int
     */
    private $contextLanguageId;

    /**
     * @var int
     */
    private $contextShopId;


    public function __construct(
        Connection $connection,
        string $dbPrefix,
        DoctrineSearchCriteriaApplicatorInterface $searchCriteriaApplicator,
        $contextLanguageId

    ) {
        parent::__construct($connection, $dbPrefix);
        $this->searchCriteriaApplicator = $searchCriteriaApplicator;
        $this->contextLanguageId = 1;
    }

    public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria): QueryBuilder
    {
        $qb = $this->getBaseQuery($searchCriteria->getFilters());
        $qb
        ->select('a.id_activity, al.title AS title, a.active');

        $this->searchCriteriaApplicator
            ->applyPagination($searchCriteria, $qb)
            ->applySorting($searchCriteria, $qb)
        ;

        return $qb;
    }

    public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria): QueryBuilder
    {
        $qb = $this->getBaseQuery($searchCriteria->getFilters());
        $qb->select('COUNT(a.id_activity)');

        return $qb;
    }

    private function getBaseQuery(array $filters): QueryBuilder
    {
        $qb =  $this->connection
            ->createQueryBuilder()
            ->from($this->dbPrefix . 'company_activities', 'a')
            ->leftJoin(
                'a',
                $this->dbPrefix . 'company_activities_lang',
                'al',
                'a.id_activity = al.id_activity AND al.id_lang = :lang_id'
            )
            ->setParameter('lang_id', $this->contextLanguageId)
        ;

        $this->applyFilters($qb, $filters);

        return $qb;
    }

    private function applyFilters(QueryBuilder $qb, array $filters)
    {
        $allowedFiltersMap = [
            'id_activity' => 'a.id_activity',
        ];

        foreach ($filters as $filterName => $value) {
            if (!array_key_exists($filterName, $allowedFiltersMap) || empty($value)) {
                continue;
            }

            $qb->andWhere($allowedFiltersMap[$filterName] . ' LIKE :' . $filterName)
                ->setParameter($filterName, '%' . $value . '%');
        }
    }

}

image.thumb.png.154df23eb48197e90414e9d6ac778189.png
If anyone has an idea I’m interested  

Link to comment
Share on other sites

my service.yml was misconfigured
I insert a new argument for the class DoctrineSearchCriteriaApplicator
 

 prestashop.core.grid.query.activity_query_builder:
    class: 'Antcompanyinfos\Core\Grid\Query\ActivityQueryBuilder'
    parent: 'prestashop.core.grid.abstract_query_builder'
    public: true
    arguments:
      - '@prestashop.core.query.doctrine_search_criteria_applicator'
      - "@=service('prestashop.adapter.legacy.context').getContext().language.id"
      - "@=service('prestashop.adapter.legacy.context').getContext().shop.id"

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...