繁体   English   中英

PHP-DI 使用先前定义的类

[英]PHP-DI using previously defined class

我正在尝试改进我的 PHP-DI 配置,但现在我卡住了。

我想为另一个类重用一个已经定义的类。

entityManager类应该来自Database->getEntityManager

这是我的 di 配置文件的一部分:

return [
    'database.user'     => 'xxxxxxx',
    'database.password' => 'xxxxxxx',
    'database.name'     => 'xxxxxxx',
    'database.host'     => '127.0.0.1',
    'database.port'     => 3306,

    \ABCData\Database\Database::class => DI\autowire()->constructor(
        DI\get('database.user'),
        DI\get('database.password'),
        DI\get('database.name'),
        DI\get('database.host'),
        DI\get('database.port')
    ),


//    \Doctrine\ORM\EntityManager::class => DI\factory(function () {
//        $database = new ABCData\Database\Database(
//            \ABCData\Database\Config::DB_USER,
//            \ABCData\Database\Config::DB_PASSWORD,
//            \ABCData\Database\Config::DB_DATABASE,
//            \ABCData\Database\Config::DB_HOST
//        );
//        return $database->getEntityManager();
//    }),


    \Doctrine\ORM\EntityManager::class => DI\autowire(\ABCData\Database\Database::class)->method('getEntityManager'),
...

当我注入数据库时​​,这工作正常。 注释定义效果很好,但我想实现最后一行但它失败了。

这是我的数据库类的完整实现:

private $userName;

private $password;

private $database;

private $host;

private $port;

public function __construct($username, $password, $database, $host, $port = 3306)
{
    $this->userName = $username;
    $this->password = $password;
    $this->database = $database;
    $this->host = $host;
    $this->port = $port;
}

public function getEntityManager()
{
    $isDevMode = true;
    $paths = [APP_DIR . 'classes/Entities'];
    $connectionCredentials = $this->getConnectionCredentials();

    $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    $entityManager = \Doctrine\ORM\EntityManager::create($connectionCredentials, $config);
    $connection = $entityManager->getConnection();

    $sqlSchemaManager = new SQLServerSchemaManager($connection);
    $sqlSchemaManager->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

    return $entityManager;
}

private function getConnectionCredentials()
{
    return [
        'driver'   => 'pdo_mysql',
        'user'     => $this->userName,
        'password' => $this->password,
        'dbname'   => $this->database,
        'host'     => $this->host,
        'port'     => $this->port
    ];
}

当我的代码需要实例化entityManager我收到此错误:

Entry "ABCData\ABCDataAPI\CronJob" cannot be resolved: Entry "ABCData\DataStructure\Save" cannot be resolved: Entry "Doctrine\ORM\EntityManager" cannot be resolved: Parameter $username of __construct() has no value defined or guessable
Full definition:
Object (
    class = ABCData\Database\Database
    lazy = false
    __construct(
        $username = #UNDEFINED#
        $password = #UNDEFINED#
        $database = #UNDEFINED#
        $host = #UNDEFINED#
        $port = (default value) 3306
    )
    getEntityManager(

    )
)

似乎它没有使用以前定义的数据库东西。

我怎么了?

好的,我误解了文档。

这就是我所做的。 如果你有更好的想法,请告诉我:

\Doctrine\ORM\EntityManager::class => DI\factory(function (\ABCData\Database\Database $database) {
    return $database->getEntityManager();
}),

暂无
暂无

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

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