尝试使用 slim 4 和 php-di 的学说,但我无法使用 autowire 运行它。 按照我的设置:
index.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
无法自动装配的提示,我将结构更改如下:
index.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),
...
userservice/core/models/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/infrastruction/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
我怎样才能让教义找到实体?
在容器中:
ProductRepository::class => function(ContainerInterface $container) {
return $container->get(EntityManager::class)->getRepository(Product::class);
}