0

Trying to use doctrine with slim 4 and php-di I don't get it running with autowire. Following my setup:

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)

then my repository:

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

Currently I get the follwoing error message:

"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
    ...

can somebody tell me how to solve that issue respectively is there any other maybe cleaner/easier way to integrate doctrine using php-di?

Update

Referring to the hint that ClassMetadata can't be autowired I changed the structure as follows:

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;
    ...

And the 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);
    }

Now I'm getting the following error in UserRepository construct (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

How can I get doctrine find the entities?

SNO
  • 793
  • 1
  • 10
  • 30
  • You're using namespaces. So you should add the `namespace ...` and the import statements (`use ...`) in your question as well. – PajuranCodes Apr 18 '20 at 18:48
  • 1
    The definition `Doctrine\ORM\Mapping\ClassMetadata::class` can't be autowired because the [constructor](https://github.com/doctrine/orm/blob/master/lib/Doctrine/ORM/Mapping/ClassMetadata.php#L182) requires a primitive data type `(public function __construct(string $entityName, ?ComponentMetadata $parent))`. May maybe you need a full cntainer definiton for the UserRepository. – odan Apr 18 '20 at 23:42
  • I managed to get Doctrine ORM 2.7.3 working with Slim 4.5.0 and PHP-DI. Ironically this question was one of the stops on my Google search journey. I will provide an answer + code if this was not resolved yet. – Peter Mghendi Jul 27 '20 at 12:18

0 Answers0