doupingyun73833 2017-07-23 22:46
浏览 37

如何在基于Symfony控制台组件的应用程序中加载包?

I am probably having some fundamental issue with understanding the Symfony Console component. I am trying to write a console-based application and I wanted to add the Doctrine bundle to it to create ORM-based entities, however, I am not able to load the bundle into my application.

What I found is that I should create app/AppKernel.php and add the bundle there:

public function registerBundles()
{
    $bundles = array(
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
    );
    return $bundles;
}

I have then also added the AppKernel to the composer.json and dumped composer's autoload files:

"classmap": [ "app/AppKernel.php" ]

However, I am still unable to see any Doctrine commands in my console application's entry point. I don't even think the Kernel is loaded properly, the most confusing part to me is the difference between these two files on Github. First you have the Console component's Application.php which uses strings for its constructor parameters:

https://github.com/symfony/console/blob/master/Application.php

And then there's the same file in the Symfony Framework and that one uses the Kernel as a parameter:

https://github.com/symfony/framework-bundle/blob/master/Console/Application.php

So am I completely on the wrong path here? How do I load bundles / Kernels in a Console Component application? Or do I need the full Symfony package to do this sort of thing? That would seem kind of overkill if I just needed to write a somewhat complex terminal application.

Just in case it is any use, these are the relevant packages in my composer.json:

"symfony/console": "v3.3.*",
"symfony/yaml": "v3.3.*",
"doctrine/orm": "~2.5",
"doctrine/doctrine-bundle": "~1.6",
"doctrine/doctrine-cache-bundle": "~1.2",
  • 写回答

1条回答 默认 最新

  • doulun1666 2017-07-24 00:50
    关注

    If you create a console command which extends the ContainerAwareCommand as described here:

    https://symfony.com/doc/current/console.html#getting-services-from-the-service-container

    Then you can call:

    $em = $this->container->get('doctrine.orm.entity_manager');
    

    Which gives you the EntityManager allowing you to use Doctrine in your command line application. There is no need to edit your AppKernel or composer file to get Doctrine working.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作