无法在symfony中自动连接服务

ctzwtxfj  于 2022-12-19  发布在  其他
关注(0)|答案(2)|浏览(118)

我正在尝试将一个大的service.yaml拆分为几个小文件。在originservice.yaml中,我有
服务:

_defaults:
    autowire: true
    autoconfigure: true
    public: false

App\Domain\Country\Infrastructure\Repository\CountryRepository:
    public: true
    class: App\Domain\Country\Infrastructure\Repository\CountryRepository
    factory: ["@doctrine.orm.default_entity_manager", getRepository]
    arguments: [App\Domain\Country\Entity\Country]

然后我在开始的时候加入了进口服务。

imports:
  - {resource: services/repositories.yaml}

repositories.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: true

     App\Domain\Country\Infrastructure\Repository\CountryRepository:
        factory: ["@doctrine.orm.default_entity_manager", getRepository]
        arguments: [App\Domain\Country\Entity\Country]

在那之后我开始出错

Cannot autowire service "App\Domain\Country\Infrastructure\Repository\Count  
  ryRepository": argument "$class" of method "Doctrine\ORM\EntityRepository::  
  __construct()" references class "Doctrine\ORM\Mapping\ClassMetadata" but no  
   such service exists.

有什么问题吗?

xdnvmnnf

xdnvmnnf1#

您不需要为了自动装配而定义存储库。
services.yaml:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

实体\国家:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CountryRepository")
 */
class Country
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }
}

存储库\国家存储库:

<?php

namespace App\Repository;

use App\Entity\Country;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Country|null find($id, $lockMode = null, $lockVersion = null)
 * @method Country|null findOneBy(array $criteria, array $orderBy = null)
 * @method Country[]    findAll()
 * @method Country[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class CountryRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Country::class);
    }
}

最后,您的服务:

<?php

namespace App\Service;

use App\Repository\CountryRepository;

class ExampleService
{
    /**
     * @var CountryRepository
     */
    private $repository;

    /**
     * @param CountryRepository $repository
     */
    public function __construct(CountryRepository $repository)
    {
        $this->repository = $repository;
    }
}

自动装配将看到您已经将CountryRepository注入到ExampleService构造函数中,并处理其余部分。

6yoyoihd

6yoyoihd2#

改用命名参数:

存储库.yaml

services:

     App\Domain\Country\Infrastructure\Repository\CountryRepository:
        factory: ["@doctrine.orm.default_entity_manager", getRepository]
        arguments:
            $class: '@App\Domain\Country\Entity\Country'

相关问题