簡體   English   中英

配置Symfony2和學說

[英]Config Symfony2 and Doctrine

無論如何,我實際上還是Symfony2和Doctrine的新手。 我正在嘗試配置我的學說,以便可以在我的Symfony2項目中使用它。

我已經在連接中創建了DatabaseRepository文件。 當我執行此文件時,出現錯誤:

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

我的數據庫存儲庫文件:

<?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;
    }
}

原則位於供應商目錄下。 我正在使用use添加連接,但這仍然無法正常工作。

好的,對不起,我認為這個問題是不完整的,我會全部解決。

這是我稱之為數據庫存儲庫的位置:

namespace Acme\DemoBundle\Repository;

use Acme\DemoBundle\Doctrine\DatabaseRepository;

class TestRepo {

    public $doctrine;

    public function __construct(
        DatabaseRepository $databaseRepository
    ){
        $this->doctrine = $databaseRepository;
    }


    public function test()
    {

    }

} 

以下是服務:

<!-- 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>

正如下面的評論所迷惑的那樣,我在我的database_repository服務中添加了參數。

但是現在我得到這個錯誤:

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"

您的database_repository配置不包含任何構造函數參數映射的argument部分。 簡而言之,您不提供所需的參數,即DatabaseRepositoryLoggerInterface實例。

有一個database_connection服務代表第一個。 有關如何在Symfony中使用DBAL的更多信息,請單擊此處。

因此,要將其添加到您的conf文件中,您應該像這樣修改它:

<service id="database_repository"
         class="Acme\DemoBundle\Doctrine\DatabaseRepository">
    <argument type="service" id="database_connection" />
</service>

您還必須添加LoggerInterface實例,但是該實例不在Symfony中,因此您必須首先為此創建一個服務。

另請參閱服務容器文檔 ,以了解服務在Symfony2中的工作方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM