Creating product from service


I’m completelly new to sylius and symfony. I’m trying to write my importer which is started through console
This is constructor of my service:

public function __construct(string $path, ProductFactoryInterface $productFactory, ProductRepositoryInterface $productRepository)

and I get error:

Cannot autowire service “App\Core\Importers\ProductImporter”: argument “$productFactory” of method “__construct()” references interface “Sylius\Component\Product\Factory\ProductFactoryInterface” but no such service exists. You should maybe alias this interface to the existing “sylius.custom_factory.product” service.

I also tried registering it in services.yml file like this:
$path: ‘%kernel.project_dir%/ress/ProductList.xml’
$productFactory: ‘@sylius.factory.product
$productRepository: ‘@sylius.repository.product

tried also:

Sylius\Component\Product\Factory\ProductFactory: ‘@sylius.factory.product
Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository: ‘@sylius.repository.product

and receive those in constructor I still get error:
Argument 2 passed to must be an instance of Sylius\Component\Product\Factory\ProductFactory, integer given

what am I doing wrong?


It tells you what to do: “You should maybe alias this interface to the existing “sylius.custom_factory.product” service.”
To understand how to autowire arguments type-hinted with interfaces read this:
And because you use a custom factory, so there are multiple implementation of that interface, read this:
If you don’t want to autowire or autoconfgure this service, read this:


Thank you for your response,
let’s make this a bit less complicated and let’s say I only want to receive


I expected that if I have this in constructor it will just work out of the box…
but then it says:

Cannot autowire service “App\Core\Importers\ProductImporter”: argument “$productRepository” of method “__construct()” references class “Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepo
sitory” but no such service exists. You should maybe alias this class to the existing “sylius.repository.product” service.

so I’m aliasing it in config with this code in services.yml:

Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository: ‘@sylius.repository.product
but then I get:
Argument 2 passed to App\Core\Importers\ProductImporter::__construct() must be an instance of Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository, integer given, called in C:\Develo
pment\acme\src\Command\ImporterCommand.php on line 34

shouldnt @sylius.repository.product be service of that type

can U give me quick basic example of this to work?
I’m following book there but can’t really understand it.
I am comming from nette framework and there as soon as something is registered in container it’s automatically passed to constructor. I see this is registered in container so I’m not sure what’s really wrong here…


also if I add this to services.yml:

     class: Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository
Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository: '@sylius.repository.product'

then I get:

Cannot autowire service “sylius.repository.product”: argument “$entityManager” of method “Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository::__construct()” references class “Doctrine\ORM\Entit
yManager” but no such service exists. Try changing the type-hint to one of its parents: interface “Doctrine\ORM\EntityManagerInterface”, or interface “Doctrine\Common\Persistence\ObjectManager”.

and this will then be a lot of definitions just to get ProductRepository from core bundle?


Can you post the full definition of that service, please?
And also the fragments where you define aliases.


Thank you for your response.
I managed to resolve issue, it was really stupid mistake from my end.
This is correct code if someone else will ever look at this thread:

Sylius\Component\Core\Repository\ProductRepositoryInterface: '@sylius.repository.product'
Sylius\Component\Product\Factory\ProductFactoryInterface: '@sylius.factory.product'
          $path: '%kernel.project_dir%/ress/ProductList.xml'

Basically I had it bellow wrong initialization manually, test code that accidentelly stayed there which had wrong params to that same class, and from there I got that issue on got integer expected ProductRepository…
But since I’m just starting with this I was stuck in config and couldn’t handle it further :smile:
couldn’t even think that issue is somewhere else and not there :smiley:
:blush: :blush: :blush: :blush: