0

I have a controller with a method calculateMaterial($product) where $product is a Doctrine entity.

I have tried to build a service for calling this method, i called my service Calculator.

Here's my services.xml file :

http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
    <service id='acv.calculator'
             class='Arto\AcvBundle\Calculator\Calculator'>
             <argument type="service" id="doctrine.orm.entity_manager"/>
             <call method="calculateMaterial">
                  <argument type="service" id="acv.product"></argument>
             </call>
    </service>
    <service id='acv.product'
             class='Arto\AcvBundle\Entity\Product'
             public='true'>
            <call method="getAssemblies"></call>
    </service>
</services>

All works fine, i call my service in a method from my controller but when i execute the calculateMaterial($product) method, which is now in my service Calculator, i always get a $product NULL.

I don't know how to do for giving the product in the method from my controller to the method of my service.

1 Answer 1

0

I don't think you should inject your $product entity in your service, but use it punctually, injecting your acv.calculator where you need it, and then call your calculateMaterial when you need it.

<?php
// ...
$caculator->calculateMaterial($product);

Especially if you need to retrieve a $product when you need it (after getting it from a repo, instanciating it, ... etc)

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, and thanks for your answer, i can inject the product id instead and then rebuild my product entity into the service but i dunno how to inject my product id parameter in my service
I'd like to inject my product id for rebuilding my product, my service will proceed to calculations on the product i'll give to it. Do you have Skype for chatting ?
Well, you got your definition of a service is wrong. A service is an object, which you should call at some point (in the controller, in another service, ...), which acts, by calling your methods. And then once you get your service, you may call your method (via $this->container->get('your.service')->calculate($product) if you're in a controller)
I have a controller full of methods which do calculations on my Product entity, i just want to externalize these methods out of the controller so i wrote a service, nope ?
Yes, but in the DIC, you do not need to define a call to you calculate method. Or declare your entity as a service. You only need to declare the calculator service. Then you call the service, and the method. Please check the documentation for more information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.