0

I am not sure if this is even best practice or possible at all.

So I have a situation where I use DataTables and I need to change a boolean value to text in order to display true/false instead of numbers. But I also need to do that in different languages.

Since I need this in several places in the app i was thinking that I should make an app specific Repository class that extends EntityRepository and use it as extended class for the repositories I am building. For this i want to inject translator object in in order to translate some keys, but translation is never set:

CustomRepository class

class CustomRepository extends EntityRepository
{
    /**
     * @var Translator
     */
    protected $translator;

    /**
     * @param Translator $translator
     */
    public function setTranslator(Translator $translator)
    {
        $this->translator = $translator; //*******this one is not set...
    }

    /**
     * Replace bool results into string values
     *
     * @param $aRes
     * @param $sField
     *
     * @return mixed
     */
    protected function _replaceBoolToStringResult(&$aRes, $sField)
    {
        if (1 == $aRes[$sField]) {
            $aRes[$sField] = str_replace('1', $this->translator->trans('site.true'), $aRes[$sField]);
        } else {
            $aRes[$sField] = str_replace('0', $this->translator->trans('site.false'), $aRes[$sField]);
        }

        return $aRes;
    }
}

services.yml

app.custom.repository:
        class: App\CommonBundle\Repository\CustomRepository
        #should i call here all the constructor vars from EntityRepository class as arguments?
        calls:
            - [setTranslator, ["@translator.default"]]

Repository with custom DQL

class SettingsRepository extends CustomRepository
{
    public function findOverviewSettingsAsJson()
    {
        $aResult = $this->createQueryBuilder('s')
                        ->select('s.identifier, s.type, s.isActive')
                        ->getQuery()
                        ->getScalarResult();

        // ******** HERE I WANT TO USE _replaceBoolToStringResult

        return json_encode($aResult);
    }
}
Herr Nentu'
  • 1,438
  • 3
  • 18
  • 49

2 Answers2

0

You must use the factory pattern when you use a repository as a service.

See possible duplicates :

Symfony 2: Creating a service from a Repository

How to inject a repository into a service in Symfony2?

Note : the syntax changed in latest SF version : http://symfony.com/doc/current/components/dependency_injection/factories.html

Edit : You should use your repository as a service :

app.custom.repository:
    class: App\CommonBundle\Repository\CustomRepository
    factory: ["@doctrine.orm.entity_manager", getRepository]
    arguments:
        - App\CommonBundle\Entity\CustomEntity
    calls:
        - [setTranslator, ["@translator.default"]]

Then call this service as any other service in your code. For example from inside a controller :

$this->get('app.custom.repository')->...
Community
  • 1
  • 1
jobou
  • 1,823
  • 2
  • 18
  • 28
  • I edited my question maybe now it's a bit clearer. I think I am not really getting clear in my head how to do this... – Herr Nentu' Jul 10 '16 at 13:33
  • This i understand. But what I am trying to achieve is to use it in other custom repositories and not in controllers. – Herr Nentu' Jul 10 '16 at 13:41
  • If you want to use this in another repository, you will have to extend your `CustomRepository` each time then create a custom service for each of these repositories. – jobou Jul 10 '16 at 13:45
  • 1
    If you don't want to always extends your CustomRepository, you could use a `trait`. But as you have a dependency to an external service, you will have to either create a `repository as a service` for each repo or call `setTranslator` manually when needed. – jobou Jul 10 '16 at 13:51
0

I found this article by Matthias to be useful on this issue. (I know link only answers are frowned on...)

craigh
  • 1,909
  • 1
  • 11
  • 24