【问题标题】:symfony4: mysql in console commandsymfony4:控制台命令中的mysql
【发布时间】:2023-04-09 05:59:01
【问题描述】:

在控制台命令中创建 mysql 连接的最佳方法是什么?

    $doctrine = $this->getContainer()->get('doctrine');
    $em = $doctrine->getEntityManager();

我想编写自己的查询,你是怎么做到的? 因为在 symfony4 文档中它使用了学说。

例如,我可以使用https://packagist.org/packages/illuminate/database,但这是一种好的做法吗?

【问题讨论】:

    标签: symfony doctrine entitymanager


    【解决方案1】:

    您总是可以使用 Doctrine 编写自己的 sql(几乎不需要)。

    https://ourcodeworld.com/articles/read/245/how-to-execute-plain-sql-using-doctrine-in-symfony-3

    然而

    $this->getContainer()
    

    在命令中不可用。我建议使用自动装配。

    /**
     * Command constructor.
     *
     * @param EntityManagerInterface $entityManager to access Doctrine
     *
     * @return void
     */
    public function __construct(
        EntityManagerInterface $entityManager
    ) : void {
    
        $this->_entityManager = $entityManager;
    
        parent::__construct();
    }
    

    此外,Doctrine 真的很强大,所以可能有一个开箱即用的解决方案。

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2014-02-23
      • 2023-03-23
      • 2016-01-29
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多