未找到 Prestashop 存储库

Prestashop Repository not found

早上好。

我在使用新的 Symfony 架构时遇到问题。 我创建了一个现代控制器,其中路由运行完美。 现在我想用 ProductRepository 搜索产品。

MyModule/src/Repository/ProductRepository

namespace PrestaShop\Module\MyModule\Repository;

use Doctrine\DBAL\Connection;

class ProductRepository
{
/**
 * @var Connection the Database connection.
 */
private $connection;

/**
 * @var string the Database prefix.
 */
private $databasePrefix;



/**
 * @param int $langId the lang id
 * @return array the list of products
 */
public function findAllbyLangId(int $langId)
{
    $prefix = $this->databasePrefix;
    $productTable = "${prefix}product";
    $productLangTable = "${prefix}product_lang";

    $query = "SELECT p.* FROM ${productTable} p LEFT JOIN ${productLangTable} pl ON (p.`id_product` = pl.`id_product`) WHERE pl.`id_lang` = :langId";
    $statement = $this->connection->prepare($query);
    $statement->bindValue('langId', $langId);
    $statement->execute();

    return $statement->fetchAll();
 }

}

MyModule/config/services.yml

services:
   product_repository:
   class: PrestaShop\Module\MyModule\Repository\ProductRepository
   arguments: ['@doctrine.dbal.default_connection', '%database_prefix%']

我的控制器

$products = $this->get('product_repository')->findAllByLangId(1);
dump($products);

现在我收到以下错误: "Attempted to load class "ProductRepository”来自命名空间 "PrestaShop\Module\MyModule\Repository"。 您是否忘记了另一个命名空间的 "use" 语句?"

我缺少什么? 感谢您的宝贵时间和帮助。

更新-堆栈跟踪:

**ClassNotFoundException**
Symfony\Component\Debug\Exception\ClassNotFoundException:
Attempted to load class "ProductRepository" from namespace 
"PrestaShop\Module\EasyUpload\Repository".
 Did you forget a "use" statement for another namespace?

 at var\cache\dev\ContainerZiol6qc\getProductRepositoryService.php:8
 at require()
 (var\cache\dev\ContainerZiol6qc\appDevDebugProjectContainer.php:1713)
 at ContainerZiol6qc\appDevDebugProjectContainer->load('getProductRepositoryService.php')(vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Container.php:304)
at Symfony\Component\DependencyInjection\Container->get('product_repository')
 (vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:67)
at Symfony\Bundle\FrameworkBundle\Controller\Controller->get('product_repository')
 (modules\easyupload\src\Controller\DemoController.php:111)
at EasyUpload\Controller\DemoController->search()
 (modules\easyupload\src\Controller\DemoController.php:76)
at EasyUpload\Controller\DemoController->indexAction(object(Request), null)(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:151)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)

(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, false)
  (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:200)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request), 1, false)
 (admin108ptrz6g\index.php:86)`

您似乎没有使用预定义的命名空间创建 composer.json 文件。

此处示例:

{
  "name": "YourName/YourModuleName",
  "description": "Awesome description",
  "autoload": {
    "psr-4": {
      "YourName\YourModuleName\": "/",
      "YourName\YourModuleName\Repository": "src/Repository/",
    }
  },
  "config": {
    "prepend-autoloader": false
  },
  "type": "prestashop-module"
}

或者如果您希望以正确的方式使用 Prestashop

{
  "name": "YourName/YourModuleName",
  "description": "Awesome description",
  "autoload": {
    "psr-4": {
      "Prestashop\Module\YourModuleName": "src/",
      "Prestashop\Module\YourModuleName\Repository": "src/Repository/",
    }
  },
  "config": {
    "prepend-autoloader": false
  },
  "type": "prestashop-module"
}

然后 运行 composer install 并添加到您的 YourModuleName.php 文件 require_once。

$autoloadPath = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloadPath)) {
    require_once $autoloadPath;
}

我也搜索了几个小时,发现我需要这个确切的结构来注册 mycompany.mymodule.myservice。这在 Prestashop 1.7.6 中必须完全像这样:

// mycompany\mymodule\config

  • 配置
    • 管理员
      • services.yml
    • 正面
      • services.yml
    • common.yml

还要确保您的命名空间设置正确。