veptune Posted April 14 Share Posted April 14 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 More sharing options...
Andrei H Posted April 18 Share Posted April 18 (edited) Hello, Does replacing: 'itemsGrid' => $grid, In the Controller with: 'itemsGrid' => $this->presentGrid($grid), changes anything? Edited April 18 by Andrei H (see edit history) Link to comment Share on other sites More sharing options...
veptune Posted April 18 Author Share Posted April 18 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 More sharing options...
Andrei H Posted April 21 Share Posted April 21 Hello, Can you upload a copy of the module? It's a lot easier debugging it that way Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now