【发布时间】: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