Good morning.
i have a problem using the new Symfony architecture. i created a modern controller where routing is working perfect. now i want to search products with ProductRepository.
MyModule/src/Repository/ProductRepository
namespace PrestaShopModuleMyModuleRepository; use DoctrineDBALConnection; 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: PrestaShopModuleMyModuleRepositoryProductRepository arguments: ['@doctrine.dbal.default_connection', '%database_prefix%']
MyController
$products = $this->get('product_repository')->findAllByLangId(1); dump($products);
Now i get the following error: “Attempted to load class “ProductRepository” from namespace “PrestaShopModuleMyModuleRepository”. Did you forget a “use” statement for another namespace?”
What im missing there? Thx for your time and help.
update – Stacktrace:
**ClassNotFoundException** SymfonyComponentDebugExceptionClassNotFoundException: Attempted to load class "ProductRepository" from namespace "PrestaShopModuleEasyUploadRepository". Did you forget a "use" statement for another namespace? at varcachedevContainerZiol6qcgetProductRepositoryService.php:8 at require() (varcachedevContainerZiol6qcappDevDebugProjectContainer.php:1713) at ContainerZiol6qcappDevDebugProjectContainer->load('getProductRepositoryService.php')(vendorsymfonysymfonysrcSymfonyComponentDependencyInjectionContainer.php:304) at SymfonyComponentDependencyInjectionContainer->get('product_repository') (vendorsymfonysymfonysrcSymfonyBundleFrameworkBundleControllerControllerTrait.php:67) at SymfonyBundleFrameworkBundleControllerController->get('product_repository') (moduleseasyuploadsrcControllerDemoController.php:111) at EasyUploadControllerDemoController->search() (moduleseasyuploadsrcControllerDemoController.php:76) at EasyUploadControllerDemoController->indexAction(object(Request), null)(vendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php:151) at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), 1) (vendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php:68) at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), 1, false) (vendorsymfonysymfonysrcSymfonyComponentHttpKernelKernel.php:200) at SymfonyComponentHttpKernelKernel->handle(object(Request), 1, false) (admin108ptrz6gindex.php:86)`
Advertisement
Answer
It’s look like you didn’t create composer.json file with predefined namespaces.
Here example:
{ "name": "YourName/YourModuleName", "description": "Awesome description", "autoload": { "psr-4": { "YourName\YourModuleName\": "/", "YourName\YourModuleName\Repository": "src/Repository/", } }, "config": { "prepend-autoloader": false }, "type": "prestashop-module" }
OR if you want with Prestashop right way
{ "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" }
Then run composer install
and add to your YourModuleName.php file require_once.
$autoloadPath = __DIR__ . '/vendor/autoload.php'; if (file_exists($autoloadPath)) { require_once $autoloadPath; }