繁体   English   中英

Symfony2教义连接问题

[英]Symfony2 Doctrine connection issue

我正在学习Symfony2,并尝试连接至dbal。 但是我遇到了一个错误,直到现在我还是无法解决。

错误信息:

Catchable fatal error: Argument 1 passed to Doctrine\DBAL\Connection::__construct() must be of the type array, none given, called in /Users/toma/Dev/api/app/cache/dev/appDevDebugProjectContainer.php on line 2313 and defined in /Users/tom/Dev/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 192

这是我在调用Doctrine / DBAL / Connection的地方:

<?php

namespace API\Test\TestDoctrine\Repository;

use API\TestBundle\TestDoctrine\DatabaseRepository;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;


class TestRepo {

    public $doctrine;

    public function __construct(
        DatabaseRepository $databaseRepository,
        Connection $connection,
        LoggerInterface $logger
    ){
        $this->doctrine = $databaseRepository;
    }


    public function test()
    {
        $test = 'Hey';
        return $test;
    }


} 

我已将该文件作为服务创建,并将其注入到要查询数据库的存储库中。 我尝试了google这个问题,但很遗憾找不到答案。

的services.xml

<service id="api.dto.template.connection"
         class="Doctrine\DBAL\Connection">
</service>

<service id="api.dto.template.logger"
         class="Psr\Log\LoggerInterface">
</service>

<service id="api.testdoctrine.database_repository"
         class="Api\TestBundle\TestDoctrine\DatabaseRepository">
    <argument type="service" id="japi.dto.template.connection" />
    <argument type="service" id="api.dto.template.logger" />
</service>

<service id="api.testdoctrine.repository.test_repo"
         class="API\TestBundle\TestDoctrine\Repository\TestRepo">
    <argument type="service" id="api.testdoctrine.database_repository" />
</service>

我不知道您要在那做什么,但是对我来说没有任何逻辑。

在Symfony 2 Standard Edition中,Doctrine 2开箱即用。 您需要做的就是在parameters.yml添加连接参数。

#app/config/parameters.yml
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: db_name
database_user: db_user
database_password: db_password

在您的控制器中,您可以像这样访问您的实体存储库:

$results = $em->getRepository('YourAppBundle:EntityName')->yourRepositoryMethod();

检查此以获得更多信息: http : //symfony.com/doc/current/book/doctrine.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM