pomm-project / pomm-bundle

Pomm2 bundle for Symfony
81 stars 31 forks source link

Question about Injection via symfony instead of Pooler #100

Closed lossendae closed 6 years ago

lossendae commented 6 years ago

Hello,

I've been using Pomm for a while, and there is only one thing that really itch me: the need to load models by using the Session.

In services you have to inject a session, most often the default one @pomm.default_session and then:

/**
 * @var $my_model MyModel
 */
$my_model = $session->getSession('default')->getModel(MyModel::class);
$my_model->findThings();
$my_model->doSomethingElse();

You can work without it, or some IDE provides external plugins that deal with similar cases (to setup for every project you're using pomm with).

I've played a little with Symfony Flex autowiring and have been able to inject Model classes with the default session directly in services requiring them by declaring the following rule in my services.yaml:

    _instanceof:
        PommProject\ModelManager\Model\Model:
            public:  true
            calls:
                - [initialize, ['@pomm.default_session']]

and modifying the initialize method a little to prevent an error :

public function initialize(Session $session)
    {
       // ...

        if (!$converter_holder->hasConverterName($this->flexible_entity_class)) {
            $converter_holder
                ->registerConverter(
                    $this->flexible_entity_class,
                    new PgEntity(
                        $this->flexible_entity_class,
                        $this->getStructure()
                    ),
                    [
                        $this->getStructure()->getRelation(),
                        $this->flexible_entity_class,
                    ]
                );
        }
    }

It works, with this i can directly autowire my models via constructor. Less code, i have my type hinting.

But, but i'm curious. What do i lose by not using the session to load my model ?

mvrhov commented 6 years ago

You do know that you can register the models as services right? (Read the last chapter of README.md)

lossendae commented 6 years ago

I didn't know.

Seems a little daunting to tag each and every models and declare them as services. I will look in the symfony doc if it's possible to automatically tag all models and model layers.

Thanks for the pointer.

mvrhov commented 6 years ago

You can tag them automatically. But I'm not using autowiring so you'll have to look at the docs on how to do that.

lossendae commented 6 years ago

I did find something akin to autotagging in the doc : https://symfony.com/blog/new-in-symfony-3-3-service-autoconfiguration

This may answer all my questions. I will test that and report it here. Should i close the issue ?

mvrhov commented 6 years ago

IMHO yes.

lossendae commented 6 years ago

As feedback, it indeed works fine with autotagging feature.

Additionally, i had to override the default session builder in yaml config to use mine:

    pomm.model_manager.session_builder:
        class: 'App\Model\Name\SessionBuilder'

Shouldn't it be precised in the documentation as well ?