user3735247
user3735247

Reputation: 11

PHP Symfony 2, use a method parameter in a service

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.

Upvotes: 0

Views: 189

Answers (1)

Talus
Talus

Reputation: 754

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)

Upvotes: 0

Related Questions