简体   繁体   中英

Symfony inject parameter in repository

I need inject parameter in repository, I try add calls to service repo but my call function not call when I call some function from my repo

    app.repository.file:
    class:            "%app.repository.file.class%"
    factory_service:  doctrine.orm.default_entity_manager
    factory_method:   getRepository
    arguments:
        - "%app.entity.file.class%"
    calls:
         - [setParam,['%md5_suffix%']]

setParam not call

    private $param;

public function setParam($param)
{
    $this->param = $param;
}

param have null

and I try use JMSDiExtraBundle

    /**
 * @DI\InjectParams({
 *     "param" = @DI\Inject("%md5_suffix%"),
 * })
 */
public function setParam($param)
{
    $this->param = $param;
}

but have in property %md5_suffix% like string, not property from parameters.yml :)

how to inject parameter from parameters yml in repo ?

Factory_service is deprecated in 2.8. Instead, try using something like:

app.repository.file:
    class: Doctrine\ORM\EntityRepository
    factory: ['@doctrine.orm.entity_manager', getRepository]
    arguments:
      - "%app.entity.file.class%"
    calls:
      - [setParam,['%md5_suffix%']]

I create another service

economy.param_helper:
    class: "%economy.param_helper.class%"
    arguments:
        - "%md5_suffix%"

and inject this service in repo like this

    /**
 * @var ParamHelper
 */
private $param;

    /**
 * @DI\InjectParams({
 *     "param" = @DI\Inject("economy.param_helper"),
 * })
 */
public function setParam(ParamHelper $param)
{
    $this->param = $param;
}

and call $md5Suffix = $this->param->getMd5Suffix();

But I'am not sure this is right way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM