Jump to content

I built a Grid and I got : Neither the property "id" nor one of the methods "id()


veptune

Recommended Posts

Hello,

I use PS 8.2

I want to use the Grid feature on my backoffice controller, to display rows from a table.
Very simple table, just 2 fields: id and label.

But I got the error:

    Neither the property "id" nor one of the methods "id()", "getid()"/"isid()"/"hasid()" or "__call()" exist and have public access in class "PrestaShop\PrestaShop\Core\Grid\Grid".

It seems it is related to the id property of the Grid object itself, not the "id" field of my rows.

When I do {{ dump(itemsGrid) }} I get:

    PrestaShop\PrestaShop\Core\Grid\Grid {
      -definition: PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinition {
        -id: "nicoitem"
        -name: "Liste des éléments"
        -columns: ColumnCollection
        -gridActions: GridActionCollection
        -bulkActions: BulkActionCollection
        -viewOptions: ViewOptionsCollection
        -filters: FilterCollection
      }
      -data: GridData {
        -records: RecordCollection {
          #items: [
            0 => [
              "id" => 1,
              "label" => "aa"
            ]
          ]
        }
        -recordsTotal: 1
        -query: "SELECT i.id, i.label FROM nicoitem i"
      }
      -searchCriteria: SearchCriteria
      -filtersForm: Symfony Form
    }

My services.yml:

    services:
      _defaults:
        public: true
        autowire: true
        autoconfigure: true
    
      NicoItem\Controller\Admin\NicoItemAdminController:
        tags: ['controller.service_arguments']
    
      # Grid presenter
      PrestaShop\PrestaShop\Core\Grid\Presenter\GridPresenterInterface:
        alias: 'prestashop.core.grid.presenter.grid_presenter'
    
      NicoItem\Grid\Definition\Factory\ItemGridDefinitionFactory: ~
    
      NicoItem\Grid\Query\ItemQueryBuilder:
        arguments:
          $connection: '@doctrine.dbal.default_connection'
          $dbPrefix: '%database_prefix%'
    
      nicoitem.grid.data_factory:
        class: PrestaShop\PrestaShop\Core\Grid\Data\Factory\DoctrineGridDataFactory
        arguments:
          - '@NicoItem\Grid\Query\ItemQueryBuilder'
          - '@prestashop.core.hook.dispatcher'
          - '@prestashop.core.grid.query.doctrine_query_parser'
          - 'nicoitem'
    
      NicoItem\Grid\Factory\ItemGridFactory:
        arguments:
          $definitionFactory: '@NicoItem\Grid\Definition\Factory\ItemGridDefinitionFactory'
          $dataFactory: '@nicoitem.grid.data_factory'
          $formFactory: '@form.factory'


My ItemGridDefinitionFactory:

    class ItemGridDefinitionFactory extends AbstractGridDefinitionFactory
    {
        public const GRID_ID = 'nicoitem';
    
        protected function getId(): string
        {
            return self::GRID_ID;
        }
    
        protected function getName(): string
        {
            return 'Liste des éléments';
        }
    
        protected function getColumns(): ColumnCollection
        {
            return (new ColumnCollection())
                ->add((new DataColumn('id'))
                    ->setName('ID')
                    ->setOptions(['field' => 'id']))
                ->add((new DataColumn('label'))
                    ->setName('Label')
                    ->setOptions(['field' => 'label']));
        }
    
        protected function getFilters(): FilterCollection
        {
            return (new FilterCollection())
                ->add((new Filter('id', TextType::class))
                    ->setTypeOptions(['required' => false, 'attr' => ['placeholder' => 'ID']]))
                ->add((new Filter('label', TextType::class))
                    ->setTypeOptions(['required' => false, 'attr' => ['placeholder' => 'Label']]));
        }
    }

My ItemQueryBuilder: 

    class ItemQueryBuilder implements DoctrineQueryBuilderInterface
    {
        public function __construct(
            private readonly Connection $connection,
            private readonly string $dbPrefix
        ) {}
    
        public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria): QueryBuilder
        {
            return $this->connection->createQueryBuilder()
                ->select('i.id', 'i.label')
                ->from('nicoitem', 'i');
        }
    
        public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria): QueryBuilder
        {
            return $this->connection->createQueryBuilder()
                ->select('COUNT(i.id)')
                ->from('nicoitem', 'i');
        }
    }

My ItemGridFactory

    class ItemGridFactory
    {
        public function __construct(
            private readonly GridDefinitionFactoryInterface $definitionFactory,
            private readonly DoctrineGridDataFactory $dataFactory,
            private readonly FormFactoryInterface $formFactory
        ) {}
    
        public function getGrid(): Grid
        {
            $definition = $this->definitionFactory->getDefinition();
            $searchCriteria = new SearchCriteria(); // no pagination
            $data = $this->dataFactory->getData($searchCriteria);
            $filtersForm = $this->formFactory->createBuilder()->getForm();
    
            return new Grid(
                $definition,
                $data,
                $searchCriteria,
                $filtersForm
            );
        }
    }

My Controller :

    public function index(
        Request $request,
        ItemGridFactory $gridFactory,
        GridPresenterInterface $presenter
    ): Response {
        $grid = $gridFactory->getGrid();
        return $this->render('@Modules/nicoitem/views/templates/admin/index.html.twig', [
            'itemsGrid' => $grid,
            'layoutTitle' => 'Liste des éléments',
        ]);
    }

They made a example module about grid, but it is for PS 9... and the documenation about PS 8 (the current version) does not show how to use a custom grid.

Any idea?

Thanks

Link to comment
Share on other sites

When I go that I got :

Warning: foreach() argument must be of type array|object, null given

in src/Core/Grid/Presenter/GridPresenter.php (line 222)

GridPresenter->isEmptyState()in src/Core/Grid/Presenter/GridPresenter.php (line 88)

GridPresenter->present()in modules/nicoitem/src/Controller/Admin/NicoItemAdminController.php (line 58)

in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php -> index (line 169)

in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php -> handleRaw (line 81)

in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php -> handle (line 201)

Kernel->handle()in admin321r0coud7kwwp2pclw/index.php (line 84)

 

at the line 'itemsGrid' => $this->presentGrid($grid),

 

 

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...