0

尝试将教义与 slim 4 和 php-di 一起使用,我无法通过 autowire 运行它。按照我的设置:

索引.php

$definitions = [
    'settings' => [
        'doctrine' => [
            'dev_mode' => true,
            'cache_dir' => __DIR__.'/../var/cache/doctrine',
            'metadata_dirs' => [__DIR__.'/../src/Domain/'],
            'connection' => [
            'driver' => 'pdo_mysql',
            'host' => 'webdb',
            'port' => 3306,
                'dbname' => 'db',
                'user' => 'user',
                'password' => 'pass',
            ]
        ]
    ],
    EntityManagerInterface::class => function (ContainerInterface $c): EntityManager {
        $doctrineSettings = $c->get('settings')['doctrine'];
        $config = Setup::createAnnotationMetadataConfiguration(
            $doctrineSettings['metadata_dirs'],
            $doctrineSettings['dev_mode']
        );
        $config->setMetadataDriverImpl(
            new AnnotationDriver(
                new AnnotationReader,
                $doctrineSettings['metadata_dirs']
            )
        );
        $config->setMetadataCacheImpl(
            new FilesystemCache($doctrineSettings['cache_dir'])
        );
        return EntityManager::create($doctrineSettings['connection'], $config);
    },
    UserRepositoryInterface::class => get(UserRepository::class)

然后我的存储库:

class UserRepository extends \Doctrine\ORM\EntityRepository implements UserRepositoryInterface {
    public function get($id){
        $user = $this->_em->find($id);
       ...
    }
}

目前我收到以下错误消息:

"Doctrine\ORM\Mapping\ClassMetadata" cannot be resolved: Parameter $entityName of __construct() has no
                value defined or guessable
                Full definition:
                Object (
                class = Doctrine\ORM\Mapping\ClassMetadata
                lazy = false
    ...

有人可以告诉我如何分别解决这个问题是否有任何其他可能更清洁/更简单的方法来使用 php-di 集成学说?

更新

参考无法自动装配的提示,ClassMetadata我将结构更改如下:

索引.php

$definitions = [
   EntityManager::class => DI\factory([EntityManager::class, 'create'])
        ->parameter('connection', DI\get('db.params'))
        ->parameter('config', DI\get('doctrine.config')),

    'db.params' => [
        'driver'   => 'pdo_mysql',
        'user'     => 'root',
        'password' => '',
        'dbname'   => 'foo',
    ],
    'doctrine.config' => Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/core/models/User"), true),
    ...

用户服务/核心/模型/User.php:

namespace userservice\core\models;

use userservice\core\exceptions\ValidationException;
use \DateTime;
use ORM\Entity;

/**
 * @Entity(repositoryClass="userservice\infrastructure\repositories\UserRepository")
 */
class User extends Model{
    /**
     * @Column(type="string", length="50")
     * @var string
     */
    private $name;
    ...

和 userservice/infrastructure/UserRepository.php:

namespace userservice\infrastructure\repositories;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\ORMInvalidArgumentException;
use userservice\core\models\User;
use userservice\core\repositories\UserRepositoryInterface;
use userservice\infrastructure\repositories\Repository;

class UserRepository extends Repository implements UserRepositoryInterface {
    private $_repository;

    /**
     * 
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager) {
        parent::__construct($entityManager);
        $this->_repository = $entityManager->getRepository('User'); // OR $this->entityManager->find('User', 1);
    }

现在我在 UserRepository 构造(getRepository)中收到以下错误:

Uncaught Doctrine\Persistence\Mapping\MappingException: Class 'User' does not exist in C:\projects\microservices\user-service\vendor\doctrine\persistence\lib\Doctrine\Persistence\Mapping\MappingException.php

我怎样才能让教义找到实体?

4

0 回答 0