[英]Config Symfony2 and Doctrine
I am new to Symfony2 and Doctrine as well actually anyway. 无论如何,我实际上还是Symfony2和Doctrine的新手。 I am trying to config my Doctrine so i can use it in my Symfony2 project.
我正在尝试配置我的学说,以便可以在我的Symfony2项目中使用它。
I have created a DatabaseRepository file where I am doing my connection. 我已经在连接中创建了DatabaseRepository文件。 When i execute this file i get an error:
当我执行此文件时,出现错误:
Catchable fatal error: Argument 1 passed to DatabaseRepository::__construct() must be an instance of Doctrine\DBAL\Connection, none given, called in app/cache/dev/appDevDebugProjectContainer.php on line 2363
My DatabaseRepository file: 我的数据库存储库文件:
<?php
namespace Acme\DemoBundle\Doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Psr\Log\LoggerInterface;
class DatabaseRepository
{
/**
* @var Connection
*/
protected $db;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param Connection $connection
* @param LoggerInterface $logger
*/
public function __construct(
Connection $connection,
LoggerInterface $logger
) {
$this->db = $connection;
$this->logger = $logger;
}
}
doctrine sits under Vendor directory. 原则位于供应商目录下。 I am using
use
to add the connection but this still will not work. 我正在使用
use
添加连接,但这仍然无法正常工作。
OK guys sorry I think this question is incomplete I will all these. 好的,对不起,我认为这个问题是不完整的,我会全部解决。
This is where I call my DatabaseRepository: 这是我称之为数据库存储库的位置:
namespace Acme\DemoBundle\Repository;
use Acme\DemoBundle\Doctrine\DatabaseRepository;
class TestRepo {
public $doctrine;
public function __construct(
DatabaseRepository $databaseRepository
){
$this->doctrine = $databaseRepository;
}
public function test()
{
}
}
And the Below are the services: 以下是服务:
<!-- Doctrine -->
<service id="database_repository"
class="Acme\DemoBundle\Doctrine\DatabaseRepository">
<argument type="service" id="Doctrine\DBAL\Connection" />
<argument type="service" id="Psr\Log\LoggerInterface" />
</service>
<service id="test_repo"
class="Acme\DemoBundle\Repository\TestRepo">
<argument type="service" id="database_repository" />
</service>
As soogested in comments below I added arguments used to my database_repository service. 正如下面的评论所迷惑的那样,我在我的database_repository服务中添加了参数。
But now I get this error: 但是现在我得到这个错误:
Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException' with message 'The service "jdatabase_repository" has a dependency on a non-existent service "doctrine\dbal\connection"
Your database_repository
configuration doesn't contain any argument
sections which are mapping for constructor parameters. 您的
database_repository
配置不包含任何构造函数参数映射的argument
部分。 In short, you don't provide requered parameters which are DatabaseRepository
and LoggerInterface
instances. 简而言之,您不提供所需的参数,即
DatabaseRepository
和LoggerInterface
实例。
There's a database_connection
service which represents the first one. 有一个
database_connection
服务代表第一个。 More info about how to use DBAL in Symfony here 有关如何在Symfony中使用DBAL的更多信息,请单击此处。
So to add it to you conf file you should modify it like this: 因此,要将其添加到您的conf文件中,您应该像这样修改它:
<service id="database_repository"
class="Acme\DemoBundle\Doctrine\DatabaseRepository">
<argument type="service" id="database_connection" />
</service>
You have also to add LoggerInterface
instance but that's out of Symfony so you have to create a service for that in first place. 您还必须添加
LoggerInterface
实例,但是该实例不在Symfony中,因此您必须首先为此创建一个服务。
Please also refer to Service Container documentation in order to understand how services work in Symfony2 另请参阅服务容器文档 ,以了解服务在Symfony2中的工作方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.