0

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

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

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

PS。我将不胜感激这个简单的例子。

4

1 回答 1

1

您只需要安装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');
    }
}

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

于 2017-07-10T08:58:46.560 回答