Doctrine\ORM\Mapping\ClassMetadata not found

MartinL
Member | 2
+
0
-

Hey, I'm new to Nette and Nettrine. I've been migrating an existing project to Nette. I decided to also use the DI now but I'm getting the following error:

Nette\DI\ServiceCreationException

Service of type Repository\UserRepository: Service of type Doctrine\ORM\Mapping\ClassMetadata required by $class in UserRepository::__construct() not found. Did you add it to configuration file?

User Repository:

<?php

namespace Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Nettrine\Extra\Repository\AbstractRepository;

class UserRepository extends AbstractRepository
{
    function __construct(\Doctrine\ORM\EntityManagerInterface $em, \Doctrine\ORM\Mapping\ClassMetadata $class)
    {
        parent::__construct($em, $class);
    }

    public function enumerate(\PagedSqlSelector $selector)
    {
        global $lang;

        $qb = $this->createQueryBuilder('user');
		...

conf/common.neon:

parameters:
    debugMode: TRUE
    database:
#        host: 10.0.2.15
        host: 127.0.0.1
        user: admin
        password: *****
        dbname: *****

extensions:
    console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
    nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
    nettrine.orm: Nettrine\ORM\DI\OrmExtension

application:
	errorPresenter:
		4xx: Error:Error4xx
		5xx: Error:Error5xx
	mapping: App\Presentation\*\**Presenter

database:
    dsn: 'mysql:host=%database.host%;dbname=%database.dbname%;charset=utf8'
    user: %database.user%
    password: %database.password%
    debugger: true
    conventions: \MsedDbConventions

latte:
	strictTypes: yes
	strictParsing: yes
	extensions:
		- App\Presentation\Accessory\LatteExtension

di:
	debugger: true
	export:
		parameters: no
		tags: no

#   classy neexistuju:
#    nettrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension(%consoleMode%)
#    nettrine.orm.attributes: Nettrine\ORM\DI\OrmAttributesExtension
#    nettrine.orm.cache: Nettrine\ORM\DI\OrmCacheExtension
#    nettrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension
#    nettrine.cache: Nettrine\Cache\DI\CacheExtension

nettrine.dbal:
    debug:
        panel: %debugMode%
    connections:
        default:
            driver: pdo_mysql
            host: %database.host%
            user: %database.user%
            password: %database.password%
            dbname: %database.dbname%
            charset: utf8

nettrine.orm:
    managers:
        default:
            classMetadataFactoryName: Doctrine\Persistence\Mapping\ClassMetadataFactory
            autoGenerateProxyClasses: %debugMode%
            connection: default
            mapping:
                App:
                    type: attributes
                    directories: [%appDir%/model/database/enity]
                    namespace: "Entity"


services:
#    - App\Core\RouterFactory::createRouter
#    - Doctrine\ORM\Mapping\ClassMetadata
#    cache: Nette\Caching\Storages\MemoryStorage

#    - Repository\UserRepository


search:
	-	in: %appDir%
		classes:
			- *Facade
			- *Factory
			- *Repository
			- *Service

index.php:

<?php

require_once("vendor/autoload.php");
require_once("app/Bootstrap.php");

$bootstrap = new App\Bootstrap;
$container = $bootstrap->bootWebApplication();	//<-- Here is where the exception is thrown

$em = $container->getService('nettrine.orm.entityManagerInterface');
...

app/Bootstrap.php:

<?php

declare(strict_types=1);

namespace App;

use Nette;
use Nette\Bootstrap\Configurator;


class Bootstrap
{
	private Configurator $configurator;
	private string $rootDir;


	public function __construct()
	{
		$this->rootDir = dirname(__DIR__);
		$this->configurator = new Configurator;
		$this->configurator->setTempDirectory($this->rootDir . '/temp');
	}


	public function bootWebApplication(): Nette\DI\Container
	{
		$this->initializeEnvironment();
		$this->setupContainer();
		return $this->configurator->createContainer();
	}


	public function initializeEnvironment(): void
	{
		//$this->configurator->setDebugMode('secret@23.75.345.200'); // enable for your remote IP
		$this->configurator->enableTracy($this->rootDir . '/log');

		$this->configurator->createRobotLoader()
			->addDirectory(__DIR__)
			->register();
	}


	private function setupContainer(): void
	{
		$configDir = $this->rootDir . '/conf';
		$this->configurator->addConfig($configDir . '/common.neon');
	}
}

composer.json:

{
    "require": {
        "tracy/tracy": "^2.10",
        "nette/di": "^3.2",
        "nette/database": "^3.2",
        "nette/caching": "^3.3",
        "nettrine/orm": "^0.10.",
        "contributte/console": "^0.10",
        "latte/latte": "^3.0",
        "nette/bootstrap": "^3.2",
        "nette/application": "^3.2",
        "nette/forms": "^3.2",
        "nette/mail": "^4.0",
        "nette/security": "^3.2",
        "nettrine/extra": "dev-master"
    },
    "require-dev": {
        "nette/tester": "^2.5.3"
    }
}

I'm not sure where should the ClassMetadata come from. I guess the DI should somehow understand the associated Entity type (\Entity\User) and create the ClassMetadata for it?

Last edited by MartinL (2025-04-13 13:47)