Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about Injection via symfony instead of Pooler #100

Closed
lossendae opened this issue Feb 28, 2018 · 6 comments
Closed

Question about Injection via symfony instead of Pooler #100

lossendae opened this issue Feb 28, 2018 · 6 comments

Comments

@lossendae
Copy link

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:

  • load each Model manually
  • assign the model to a variable, and use use phpdoc to map the variable to the model class and benefit from nice things like methods from the object in almost all IDE (or a method with the model as return signature)
/**
 * @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
Copy link
Contributor

mvrhov commented Mar 1, 2018

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

@lossendae
Copy link
Author

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
Copy link
Contributor

mvrhov commented Mar 1, 2018

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
Copy link
Author

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
Copy link
Contributor

mvrhov commented Mar 2, 2018

IMHO yes.

@lossendae
Copy link
Author

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants