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 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%']
MyController
$products = $this->get('product_repository')->findAllByLangId(1);
dump($products);
Now i get the following error: "Attempted to load class "ProductRepository" from namespace "PrestaShop\Module\MyModule\Repository". Did you forget a "use" statement for another namespace?"
What im missing there? Thx for your time and help.
update - Stacktrace:
**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)`
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;
}