首页 > 解决方案 > Symfony 表实体本身也被识别为存储库

问题描述

Symfony 和 StackOverflow 作为 OP 的新手。

我正在尝试在 Symfony(4.3.2) 上添加新的 mysql 表实体和存储库,并在测试时卡住了。
我真的很感谢有人的帮助。

我得到的错误是

[2020-07-13 03:25:59] request.CRITICAL: Uncaught PHP Exception RuntimeException: "The "App\Entity\Foo" entity has a repositoryClass set to "App\Repository\Foo", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service"." at /var/www/html/bar_project/vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php line 66 {"exception":"[object] (RuntimeException(code: 0): The \"App\\Entity\\Foo\" entity has a repositoryClass set to \"App\\Repository\\Foo\", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with \"doctrine.repository_service\". at /var/www/html/bar_project/vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php:66)"} []

其中Foo/App/Entity/ 是 table 的实体foo
问题是它试图Foo在 App/Repository/ 处引用它的存储库,它不存在,但不FooRepository存在。(根据来自的评论更新了本段Jakumi

(FooController.php)

$repository = $this->getDoctrine()->getRepository(Foo::class);
(Foo.php)

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="foo")
 * @ORM\Entity(repositoryClass="App\Repository\FooRepository") // *1
 */
class Foo
{
...
(FooRepository.php)

<?php

namespace App\Repository;

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

/**
 * @method Foo|null find($id, $lockMode = null, $lockVersion = null)
 * ...
 */
class FooRepository extends ServiceEntityRepository
{
...

我觉得神秘的一件事是,即使我删除了相同的错误日志,仍然会显示*1.
(我认为我的纱线手表和缓存清除正常工作。)


附加信息 1

(doctrine.yaml)

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '8.0'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci
        # dbname:
        # host:
        # port:
        # user:
        # password:
        url: '%env(resolve:DATABASE_URL)%'
        options:
          1002: 'SET sql_mode=(SELECT REPLACE(@@sql_mode, "ONLY_FULL_GROUP_BY", ""))'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        metadata_cache_driver: apcu
        result_cache_driver: apcu
        query_cache_driver: apcu
        connection: ~
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
        dql:
          string_functions:
            group_concat: DoctrineExtensions\Query\Mysql\GroupConcat
            date_format: DoctrineExtensions\Query\Mysql\DateFormat

标签: phpsymfonydoctrine-ormdoctrine

解决方案


推荐阅读