【问题标题】:Symfony: The class XYZ was not found in the chain configured namespacesSymfony:在链配置的命名空间中找不到类 XYZ
【发布时间】:2019-03-12 09:13:15
【问题描述】:

现有的问题没有帮助。 我仍在学习 Symfony 并建立一个现有项目。试图从我的应用程序目录运行教义装置。

./app/console doctrine:fixtures:load --env test

运行它会给我以下错误

运行命令“doctrine:fixtures:load --env test --em default”时抛出错误。消息:“在链配置的命名空间 _XyzProjectName\CalendarContext\Domain\Model、Integration\GetFeedbackBundle\Model、_XyzProjectName\AccountingProcessContext\Domain\Model、_XyzProjectName\CaseContext\Domain 中找不到类 'ClientPortal\GenericBundle\Document\Accountant'” {"exception":"[object] (Doctrine\Common\Persistence\Mapping\MappingException(code: 0): 在链配置的命名空间_XyzProjectName\CalendarContext\Domain\ 中找不到类'ClientPortal\GenericBundle\Document\Accountant'模型、Integration\GetFeedbackBundle\Model、_XyzProjectName\AccountingProcessContext\Domain\Model、_XyzProjectName\CaseContext\Domain 在 /usr/local/var/www/1800-api/vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping /MappingException.php:22)

当试图持久化类的对象时抛出这个异常

'ClientPortal\GenericBundle\Document\Accountant'

这是我在 config.yml 中的教义

doctrine:
dbal:
    driver:   pdo_pgsql
    host:     '%database_host%'
    dbname:   '%database_name%'
    user:     '%database_user%'
    password: '%database_password%'
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    auto_mapping: true
    mappings:
        CalendarBundle:
            prefix: '_XyzProjectName\CalendarContext\Domain\Model'
        IntegrationGetFeedbackBundle:
            prefix: 'Integration\GetFeedbackBundle\Model'
        AccountingProcessBundle:
            prefix: '_XyzProjectName\AccountingProcessContext\Domain\Model'
        CaseBundle:
            prefix: '_XyzProjectName\CaseContext\Domain'
        GenericBundle:
            type: "annotation"   
            prefix: 'ClientPortal\GenericBundle\Document'
            dir: 'src'
            is_bundle: false

引发此异常的 sn-p

    $account = new ClientPortal\GenericBundle\Document\Accountant();//full namespace added for clarity - also this file lies in the same directory structure. 
    $account->setSfAccountantId($accountantId);
    $account->setUsername($username);
    $manager = new ObjectManager;
    $manager->persist( $account ); //This throws the above mentioned exception

我的想法是,文件 ClientPortal\GenericBundle\Document\Accountant.php 正在自动加载,因为在其对象实例化时没有抛出异常。但是,配置或映射缺少一些东西,因为学说不知道如何持久化它的对象。

[源码][1]会计类

在 AppKernel 下注册的 GenericBundle(s)

new Website\GenericBundle\WebsiteGenericBundle(),
new Admin\GenericBundle\AdminGenericBundle(),
new ClientPortal\GenericBundle\ClientPortalGenericBundle(),
new TaxApp\GenericBundle\TaxAppGenericBundle(),

【问题讨论】:

  • 嗨,您是否尝试将ClientPortal\GenericBundle\Document 添加到教义配置中的mappings 部分?
  • 我们对 ClientPortal\GenericBundle\Document 配置一无所知。请提供更多信息。
  • @EugeneR 是的,我已经尝试在那里添加它。你能用确切的语法建议你的方法吗?
  • @vytsci 嘿,我不确定你到底需要什么。但是我添加了一些关于调用各个方法和目录结构的信息。如果您需要任何东西,请告诉我。
  • @Aman ClientPortal\GenericBundle\Document like EugeneR 建议尝试将其添加到映射中。因为它没有实体命名空间所以学说是看不到的。

标签: symfony


【解决方案1】:

所以,正如我在 cmets 中建议的那样 - 尝试将 ClientPortal\GenericBundle\Document\ 命名空间添加到您的学说配置文件中的映射中,如下所示:

#...
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    mappings:
        # ...
        ClientPortalGenericBundle:
            type: annotation
            prefix: 'ClientPortal\GenericBundle\Document\'
            dir: '%kernel.root_dir%/src/ClientPortal/GenericBundle/Document/'
            #is_bundle: false
    auto_mapping: true

【讨论】:

  • 好吧,我之前已经试过了。它会抛出“Unknown bundle GenericBundle or not enabled”
  • 我尝试添加它。更新了有问题的配置,这是现在抛出的错误Class "ClientPortal\GenericBundle\Document\Accountant" is not a valid entity or mapped super class." {"exception
  • 谢谢哥们。有效。我必须添加一些其他配置属性以及类型、前缀和目录。
  • @Aman,很高兴能提供帮助。您能否提供您添加的这些附加参数 - 我将在答案中添加它
  • 我还在深入研究。也许我很困惑。它仍然无法正常工作。关于附加参数,您可以在问题中看到它们。我已经更新了相关配置。
【解决方案2】:

在使用多个数据库连接时,Symfony 4 也有同样的问题。这个https://stackoverflow.com/a/55361742/2540699 帮我解决了我的问题。

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2023-03-02
    • 2015-11-12
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多