未捕获的 PHP 异常 Doctrine\ORM\ORMException:“未知实体命名空间别名‘AppBundle’。”

2024-03-25

我刚刚开始使用 Composer 开始我的第一个项目,并想为其设置数据库和类。然而我被困住了。我在 prod.log 中收到上述错误

我在这里遵循了本教程:http://symfony.com/doc/current/book/doctrine.html http://symfony.com/doc/current/book/doctrine.html

我创建了数据库

php bin/console doctrine:database:create

然后想创建一个实体

php bin/console doctrine:generate:entity

当被问及The Entity shortcut name我进入了AppBundle:Product

然后创建数据库字段等。

我收到这条消息

实体生成

生成实体类 src/AppBundle/Entity/Product.php:好的!
生成存储库类 src/AppBundle/Repository/ProductRepository.php:好的!

一切都好!现在开始工作吧:)。

所以这听起来好像一切都有效,对吧?

现在在我的ProductController我用过这个

$products = $this->getDoctrine()
    ->getRepository('AppBundle:Product')
    ->findAll();

我收到错误

[2016-02-15 18:56:14] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Unknown Entity namespace alias 'AppBundle'." at /home/vagrant/work/homestead/test/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 271 {"exception":"[object] (Doctrine\\ORM\\ORMException(code: 0): Unknown Entity namespace alias 'AppBundle'. at /home/vagrant/work/homestead/test/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:271)"} []

我也尝试过

->getRepository('AppBundle\Entity\Product')

但我在这里收到消息

[2016-02-15 19:01:39] request.CRITICAL: Uncaught PHP Exception Doctrine\Common\Persistence\Mapping\MappingException: "The class 'AppBundle\Entity\Product' was not found in the chain configured namespaces " at /home/vagrant/work/homestead/test/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 37 {"exception":"[object] (Doctrine\\Common\\Persistence\\Mapping\\MappingException(code: 0): The class 'AppBundle\\Entity\\Product' was not found in the chain configured namespaces  at /home/vagrant/work/homestead/test/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:37)"} []

我实际上如何让它发挥作用/我刚刚开始该项目。不过,教义创建了类。我有这样的

/src
/src/AppBundle/
/src/AppBundle/Entity
    Product.php
/src/AppBundle/Repository
    ProductRepository.php

另外,在 Product.php 中,这是在注释的注释中(如果有帮助的话)

 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")

另外,当尝试这个命令时php bin/console doctrine:generate:entities AppBundle重新创建实体我没有收到任何错误消息。

我越来越

Generating entities for bundle "AppBundle"
 > backing up Product.php to Product.php~
 > generating AppBundle\Entity\Product

解决方案是删除缓存文件,因此如果您在生产模式下工作,则需要它。在调试模式下,这是自动完成的。当然,如果您使用的是 3.x,则手动执行此操作,只需将 prod 文件夹放到 var 下,如果您使用的是 2.x,则将其放在 app 下。

2.x 清除缓存的命令是:

php app/console cache:clear --env=prod
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未捕获的 PHP 异常 Doctrine\ORM\ORMException:“未知实体命名空间别名‘AppBundle’。” 的相关文章

随机推荐