How to use my Entities and Entity Managers in Symfony 2 Console Command?

后端 未结 3 1402
面向向阳花
面向向阳花 2020-12-29 01:34

I want to a few terminal commands to my Symfony2 application. I\'ve gone through the example in the cookbook, but I couldn\'t find out how to access my settings, my entity m

3条回答
  •  执念已碎
    2020-12-29 02:21

    I know that Matt's answer solved the question, But if you've more than one entity manager, you can use this:

    Make model.xml with:

    
    
    
    
    
        
    
    
    

    Then load this file in your DI extension

    $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('model.xml');
    

    Then you can use it anywhere. In Console Command execute:

    $em = $this->getContainer()->get('EM_NAME.entity_manager');
    

    and don't forget at end to :

    $em->flush();
    

    You can now use it as a argument in another service in services.yml:

    services:
        SOME_SERVICE:
            class: %parameter.class%
            arguments:
                - @EM_NAME.entity_manager
    

    Hope this help someone.

提交回复
热议问题