Symfony 3 MicroKernel 和数据库连接

Symfony 3 MicroKernel and db connection

我想用 MicroKernelTrait 创建一个 symfony 应用程序。我对学说和创建查询有疑问。

我用这个例子(单个文件): https://symfony.com/doc/current/configuration/micro_kernel_trait.html

我应该如何配置数据库(是否需要单独的文件)以及我需要哪些包?

PS。我将感谢这个简单的例子。

您只需安装 DoctrineBundle,然后注册并配置它:

$ composer require doctrine/doctrine-bundle
//index.php
//…
class AppKernel extends Kernel
{
    //…
    public function registerBundles()
    {
        return array(
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
        );
    }
    //…
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
    {
        //…
        // in-file config
        $c->loadFromExtension('doctrine', array(
            'dbal' => array(
                'driver' => 'pdo_mysql',
                'host' => '127.0.0.1',
                'port' => null,
                'dbname' => 'symfony',
                'user' => 'root',
                'password' => 'Pa$$w0rd',
                'charset' => 'UTF8'
            )
        ));
        // or from-file config
        // $loader->load(__DIR__.'/config/doctrine.yml');
    }
}

之后,您可以通过$this->container->get('doctrine');访问Doctrine。