【问题标题】:Kayue\WordpressBundle with Symfony 4.4.1: The class XXX was not found in the chain configured namespaces App\Entity带有 Symfony 4.4.1 的 Kayue\WordpressBundle:在链配置的命名空间 App\Entity 中找不到类 XXX
【发布时间】:2019-12-24 17:17:23
【问题描述】:

我正在尝试在我的 Symfony 4.4.1 项目中使用 kayue/KayueWordpressBundle 作为 composer require kayue/kayue-wordpress-bundle 安装作曲家,但我无法做到。

这就是我想要做的:

<?php

namespace App\Service\WordPress;

use Doctrine\ORM\EntityManagerInterface;
use Kayue\WordpressBundle\Entity\Post;

class PostCollection
{
    protected $postRepository;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->postRepository = $entityManager->getRepository(Post::class);
    }
}

我得到的错误:

The class 'Kayue\WordpressBundle\Entity\Post' was not found in the chain configured namespaces App\Entity

At first I blamed my dual-database configuration(Symfony 与 Wordpress 位于不同的数据库中)但后来我将数据库放在一起,问题仍然存在:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # Only needed for MySQL (ignored otherwise)
        charset: utf8mb4
        default_table_options:
            collate: utf8mb4_unicode_ci
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

过去 2 小时我一直在摆弄,但现在我没有什么想法了。我想知道是否有人真的让这个与 Symfony 4 一起工作。

谢谢!

编辑:其他尝试:

直接后注入:

use Kayue\WordpressBundle\Entity\Post;
public function index(Post $post){}

结果:

无法自动装配“App\Controller\IndexController::index()”的参数 $post:它引用了类“Kayue\WordpressBundle\Entity\Post”,但不存在此类服务。

根据文档:过时的 Symfony 2 方式

$repo = $this->get('kayue_wordpress')->getManager()->getRepository('KayueWordpressBundle:Post');

结果:

Service "kayue_wordpress" not found:即使它存在于应用程序的容器中,但 "App\Controller\IndexController" 内的容器是一个较小的服务定位器,只知道 "doctrine"、"form.factory" ”、“http_kernel”、“parameter_bag”、“request_stack”、“router”、“security.authorization_checker”、“security.csrf.token_manager”、“security.token_storage”、“serializer”、“session”和“twig”服务.尝试使用依赖注入。

做到这一点的“最佳方式”实际上是:

    public function index(EntityManagerInterface $entityManager)
    {
$entityManager->getRepository('KayueWordpressBundle:Post');
}

结果:

在链配置的命名空间 App\Entity 中找不到类“Kayue\WordpressBundle\Entity\Post”

【问题讨论】:

  • 您是否使用 composer 来安装包?那应该照顾自动加载。你可以试试“composer dump-autoload”,虽然它应该是自动运行的。
  • 是的,我安装了与作曲家一起安装的composer require kayue/kayue-wordpress-bundle(问题已通过此步骤更新,谢谢)。 Flex 已执行(我在 bundles.php 中看到)。
  • 你自己做了一个 config/packages/kayue_wordpress.yaml 文件吗?因为如果没有食谱,Flex 不会为您做到这一点。不过可能不是问题。
  • 是的,我自己创建了 config/packages/kayue_wordpress.yaml 文件。它只包含连接名称。

标签: symfony doctrine-orm doctrine symfony4 entitymanager


【解决方案1】:

虽然您找到了解决方案,但我想解释一连串问题。

错误

在链配置的命名空间 App\Entity 中找不到类“Kayue\WordpressBundle\Entity\Post”

表示在实体管理器中提供,其配置定义在:

orm:
    ...
    mappings:
        App:
        ...
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App

找不到实体类型Kayue\WordpressBundle\Entity\Post

通常这种类型的错误是通过以下方式解决的:

  • 在实体管理器中包含路径 Kayue\WordpressBundle\Entity
  • 使用包含此路径的另一个实体管理器

在您的情况下,默认实体管理器是自动装配的,基于 Doctrine\ORM\EntityManagerInterface 的服务别名,如 here 所述。别名在doctrine bundle `s config 中定义,指向默认的学说实体管理器。

你想使用 Kayue\WordpressBundle 的实体管理器,而不是默认的

解决方案

要解决这个问题,你可以

1) Bind Arguments By type,如您所见,为服务 kayue_wordpress 创建一个别名 Kayue\WordpressBundle\Wordpress\ManagerRegistry,如下:

services:
      # pass this service for any ManagerRegistry type-hint for any
      # service that's defined in this file
      Kayue\WordpressBundle\Wordpress\ManagerRegistry: '@kayue_wordpress'

2) 使用Binding Arguments by Name,在本例中为“$wpManagerRegistry”,如:

services:
# default configuration for services in *this* file
    _defaults:
        ...
        bind:
            $wpManagerRegistry: '@kayue_wordpress'

然后

public function index($wpManagerRegistry)
{

    $postRepository = $wpManagerRegistry->getManager()->getRepository('KayueWordpressBundle:Post');

以便任何名称为“$wpManagerRegistry”的参数自动连接到此服务。

参考文献

The Symfony 3.3 DI Container Changes Explained (autowiring, _defaults, etc)

【讨论】:

    【解决方案2】:

    我的同事找到了解决方案。您必须像这样配置自动装配:

    // config/packages/kayue_wordpress.yaml
    
    services:
      Kayue\WordpressBundle\Wordpress\ManagerRegistry: '@kayue_wordpress'
    

    之后,您可以自动接线:

    use Kayue\WordpressBundle\Wordpress\ManagerRegistry;
    
        public function __construct(ManagerRegistry $wpManagerRegistry)
        {
            $this->wpPostRepository = $wpManagerRegistry->getManager()->getRepository('KayueWordpressBundle:Post');
        }
    
    public function getPosts()
    {
    $post = $this->wpPostRepository->findOneBy(array(
            'slug'   => 'hello-world',
            'type'   => 'post',
            'status' => 'publish',
        ));
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2021-08-09
      • 2021-12-27
      • 2019-08-17
      • 2019-08-10
      • 2019-08-23
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      相关资源
      最近更新 更多