Jump to content

Sviluppo Modulo e Doctrine Repository


mrav

Recommended Posts

Salve a tutti,

sto scoprendo Prestashop e mi sono avviato alla creazione di un modulo personalizzato seguendo la documentazione ufficiale.

In particolare, sto cercando di utilizzare Doctrine per recuperare i prodotti dal database, estraendo solo alcuni campi e filtrandoli in base alle mie esigenze. Ho quindi creato i seguenti files.

Product.php

<?php

namespace MyModule\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MyModule\Entity\Repository\ProductRepository")
 */
class Product
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(name="id_product", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var Collection ProductLang
     * @OneToMany(targetEntity="ProductLang", mappedBy="id_product")
     */
    private $locale;

    public function getId(): int
    {
        return $this->id;
    }

    public function getLocale(): Collection
    {
        return $this->locale;
    }
}

ProductRepository.php

<?php declare(strict_types=1);

namespace MyModule\Entity\Repository;
use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    public function getAll()
    {
        $query = $this->createQueryBuilder('p')
            ->where('p.visibility = "none"')
            ->andWhere('p.is_virtual = true');

        return $query->getQuery()->getResult();
    }
}

services.yml

services:

  mymodule.entity.repository.productrepository:
    class: MyModule\Entity\Repository\ProductRepository
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - MyModule\Entity\Product

MyModuleController.php

<?php declare(strict_types=1);

namespace MyModule\Controller\Admin;

use PrestaShopBundle\Security\Annotation\ModuleActivated;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use MyModule\Entity\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class RentController.
 *
 * @ModuleActivated(moduleName="mymodule", redirectRoute="admin_module_manage")
 */
class MyModuleController extends FrameworkBundleAdminController
{
    private ProductRepository $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
        parent::__construct();
    }

    /**
     * @AdminSecurity("is_granted('read', request.get('_legacy_controller'))", message="Access denied.")
     *
     * @return Response
     */
    public function productsAction()
    {
        $products = $this->productRepository->getAll();
        return $this->render('@Modules/mymodule/views/templates/admin/products.html.twig', [
            'products' => $products
        ]);
    }
}

 

Il problema che riscontro è che quando eseguo il metodo productsAction(), ottengo il seguente errore:

[Syntax Error] line 0, col 63: Error: Expected Literal, got '"'

e nello stack trace mi ritrovo questo:

Doctrine\ORM\Query\ QueryException

SELECT p FROM MyModule\Entity\Product p WHERE p.is_virtual = true

Da quello che capisco, non è in grado di risolvere il nome della tabella dall'entità.

Eppure, come specificato qui, la conversione dovrebbe essere trasparente. Difatti, se istanzio il repository tramite l'entityManager "manualmente"

/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$productRepository = $entityManager->getRepository(Product::class);

i prodotti li recupero.

Cosa mi sfugge?

 

Grazie a quanti,

Mario.

Edited by mrav (see edit history)
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...