【问题标题】:ReflectionException: Class does not exist after upgrading SymfonyReflectionException: 升级 Symfony 后类不存在
【发布时间】:2020-01-02 16:12:57
【问题描述】:

在将我的 php 版本从 5.6 升级到 7.3 并将 Symfony 从 3.4 升级到 4.4 后,每次尝试访问本地主机上的应用程序时都会收到 ReflectionException。

    ReflectionException:
    Class WaiverBundle\File does not exist

      at /srv/http/sp7/vendor/jms/metadata/src/MetadataFactory.php:170
      at ReflectionClass->__construct('WaiverBundle\\File')
         (/srv/http/sp7/vendor/jms/metadata/src/MetadataFactory.php:170)
      at Metadata\MetadataFactory->getClassHierarchy('WaiverBundle\\File')
         (/srv/http/sp7/vendor/jms/metadata/src/MetadataFactory.php:76)
      at Metadata\MetadataFactory->getMetadataForClass('WaiverBundle\\File')
         (/srv/http/sp7/vendor/vich/uploader-bundle/src/Metadata/MetadataReader.php:85)
      at Vich\UploaderBundle\Metadata\MetadataReader->getUploadableFields('WaiverBundle\\File')
         (/srv/http/sp7/vendor/vich/uploader-bundle/src/DataCollector/MappingCollector.php:27)
      at Vich\UploaderBundle\DataCollector\MappingCollector->collect(object(Request), object(Response), object(ReflectionException))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php:178)
      at Symfony\Component\HttpKernel\Profiler\Profiler->collect(object(Request), object(Response), object(ReflectionException))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php:90)
      at Symfony\Component\HttpKernel\EventListener\ProfilerListener->onKernelResponse(object(ResponseEvent), 'kernel.response', object(TraceableEventDispatcher))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php:126)
      at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(ResponseEvent), 'kernel.response', object(TraceableEventDispatcher))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:264)
      at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.response', object(ResponseEvent))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:239)
      at Symfony\Component\EventDispatcher\EventDispatcher->callListeners(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.response', object(ResponseEvent))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:73)
      at Symfony\Component\EventDispatcher\EventDispatcher->dispatch(object(ResponseEvent), 'kernel.response')
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:168)
      at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch(object(ResponseEvent), 'kernel.response')
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:179)
      at Symfony\Component\HttpKernel\HttpKernel->filterResponse(object(Response), object(Request), 2)
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:167)
      at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 2)
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
      at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 2, false)
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php:61)
      at Symfony\Component\HttpKernel\EventListener\ExceptionListener->onKernelException(object(ExceptionEvent), 'kernel.exception', object(TraceableEventDispatcher))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php:126)
      at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(ExceptionEvent), 'kernel.exception', object(TraceableEventDispatcher))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:264)
      at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.exception', object(ExceptionEvent))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:239)
      at Symfony\Component\EventDispatcher\EventDispatcher->callListeners(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.exception', object(ExceptionEvent))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:73)
      at Symfony\Component\EventDispatcher\EventDispatcher->dispatch(object(ExceptionEvent), 'kernel.exception')
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:168)
      at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch(object(ExceptionEvent), 'kernel.exception')
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:207)
      at Symfony\Component\HttpKernel\HttpKernel->handleThrowable(object(ReflectionException), object(Request), 1)
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:100)
      at Symfony\Component\HttpKernel\HttpKernel->terminateWithException(object(ReflectionException), object(Request))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php:116)
      at Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::Symfony\Component\HttpKernel\EventListener\{closure}(object(ReflectionException))
         (/srv/http/sp7/vendor/symfony/symfony/src/Symfony/Component/ErrorHandler/ErrorHandler.php:592)
      at Symfony\Component\ErrorHandler\ErrorHandler->handleException(object(ReflectionException))                

WaiverBundle\Entity\File.php


    <?php

namespace WaiverBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\File as BaseFile;

/**
 * This class extends the base file class. Only the mapped entity and table name are different.
 *
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="waiver_file")
 * @ORM\Entity
 */
class File extends BaseFile {
    /**
     * @ORM\ManyToOne(targetEntity="WaiverBundle\Entity\Waiver", inversedBy="file")
     * @ORM\JoinColumn(name="entity", referencedColumnName="id", nullable=true)
     * @var \WaiverBundle\Entity\Waiver
     **/
    protected $entity;
}
*emphasized text*

AppBundle\Entity\File.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * This class defines attachments throughout the application.
 *
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks()
 */
class File {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     * It is required for VichUploaderBundle and holds the actual file.
     * DO NOT rename this property since its name is hardcoded in several files.
     *
     * @var Symfony\Component\HttpFoundation\File\File
     * @Assert\File()
     */
    private $attachment;

    /**
     * @var string
     * @ORM\Column(type="string", nullable=false)
     */
    private $filename;

    /**
     * @var DateTime
     * @ORM\Column(type="datetime")
     */
    private $timestamp;

    /**
     * @ORM\ManyToOne(targetEntity="\UserBundle\Entity\User")
     * @ORM\JoinColumn(name="uploader", referencedColumnName="id")
     * @var \UserBundle\Entity\User
     */
    private $uploader;

    // GETTERS/SETTERS ETC.

    /**
     * @ORM\PrePersist
     */
    public function prePersist() {
        // set the timestamp for last update
        date_default_timezone_set('UTC');
        $this->timestamp = new \DateTime("now");
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param \Symfony\Component\HttpFoundation\File\File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
     *
     * @return File
     */
    public function setAttachment(\Symfony\Component\HttpFoundation\File\File $file = null)
    {
        // store file in file system
        $this->attachment = $file;

        if ($file) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            date_default_timezone_set('UTC');
            $this->timestamp = new \DateTime("now");
        }

        return $this;
    }

    /**
     * @return Symfony\Component\HttpFoundation\File\File|null
     */
    public function getAttachment()
    {
        return $this->attachment;
    }

    /**
     * NOTE: This is not a Doctrine getter. It is just a workaround to get the original file name (see issue #319)
     * Cut off filename up to and including the first underscore
     *
     * @return string
     */
    public function getOriginalFilename()
    {
        return substr($this->filename, strpos($this->filename, '_') + 1);
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set filename
     *
     * @param string $filename
     *
     * @return File
     */
    public function setFilename($filename)
    {
        $this->filename = $filename;

        return $this;
    }

    /**
     * Get filename
     *
     * @return string
     */
    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * Set timestamp
     *
     * @param \DateTime $timestamp
     *
     * @return File
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;

        return $this;
    }

    /**
     * Get timestamp
     *
     * @return \DateTime
     */
    public function getTimestamp()
    {
        return $this->timestamp;
    }

    /**
     * Set uploader
     *
     * @param \UserBundle\Entity\User $uploader
     *
     * @return File
     */
    public function setUploader(\UserBundle\Entity\User $uploader = null)
    {
        $this->uploader = $uploader;

        return $this;
    }

    /**
     * Get uploader
     *
     * @return \UserBundle\Entity\User
     */
    public function getUploader()
    {
        return $this->uploader;
    }

    /**
     * Set entity
     *
     * @param \AppBundle\Entity\FileAwareEntity $entity
     *
     * @return File
     */
    public function setEntity(\AppBundle\Entity\FileAwareEntity $entity = null)
    {
        $this->entity = $entity;

        return $this;
    }

    /**
     * Get entity
     *
     * @return \AppBundle\Entity\FileAwareEntity
     */
    public function getEntity()
    {
        return $this->entity;
    }
}

composer.json

{
    "name": "stefan/sp28",
    "license": "proprietary",
    "type": "project",
    "autoload": {
        "psr-4": {
            "": "src/",
            "AppBundle\\": "src/AppBundle",
            "WaiverBundle\\": "src/WaiverBundle",
            "Mindpool\\LogBundle\\": "vendor/mindpool/logbundle"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "require": {
        "php": ">=7.1.3",
        "symfony/symfony": "^4.4.0",
        "doctrine/orm": "^2.7",
        "doctrine/doctrine-bundle": "~1.4",
        "symfony/swiftmailer-bundle": "~3.4",
        "symfony/monolog-bundle": "~3.1",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/user-bundle": "^2.1.2",
        "twig/twig": "^2.9",
        "salavert/time-ago-in-words": "^1.7.1",

        "stof/doctrine-extensions-bundle": "^1.2",
        "vich/uploader-bundle": "~1.12.1",
        "knplabs/knp-paginator-bundle": "^5.0",
        "mgilet/notification-bundle": "^3.1",

        "php-translation/symfony-bundle": "^0.6.1",
        "friendsofsymfony/elastica-bundle": "5.1",
        "ruflin/elastica": "^6",
        "friendsofsymfony/ckeditor-bundle": "^1.1",
        "paragonie/random_compat": "~2.0.18",
        "pagerfanta/pagerfanta": "^1.1",
        "harvesthq/chosen": "^1.8",

        "symfony/cache": "^4.0",
        "symfony/config": "^4.0",
        "symfony/console": "^4.0",
        "symfony/debug": "^4.0",
        "symfony/dependency-injection": "^4.0",
        "symfony/dotenv": "^4.0",
        "doctrine/annotations": "^1.8",
        "twig/extensions": "^1.5",
        "sensio/framework-extra-bundle": "^5.5",
        "jms/metadata": "^2.1"
    },
    "require-dev": {
        "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "reset-bootstrap-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap"
            ]
    },
    "config": {
        "bin-dir": "bin",
        "platform": {
            "php": "7.3"
        }
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "symfony": {
            "allow-contrib": false,
            "require": "4.4.*"
        }
    }
}

我没有对使用的文件进行任何更改,WaiverBundle\File 仍然存在于目录“WaiverBundle\Entity\File”中。我还删除了缓存并重新启动了 Web 服务器,但仍然收到错误消息。 我怀疑它可能与我使用的 VichUploaderBundle 有关,但我在升级过程中没有对其进行任何更改。

有人知道如何摆脱这个错误吗?

【问题讨论】:

    标签: php symfony vichuploaderbundle


    【解决方案1】:

    您已经回答了自己的问题。

    […] WaiverBundle\File 仍然存在于目录“WaiverBundle\Entity\File”中。

    这些不是一回事,从跟踪来看,您似乎有一个引用 WaiverBundle\File 而不是 WaiverBundle\Entity\File 的注释。

    【讨论】:

    • 我搜索了任何引用 WaiverBundle\File 的注释,但没有找到。你有什么其他想法我的错误可能来自哪里?
    • 您在 WaiverBundle\Entity\File 中有一个文件,但是将它与命名空间 auf WaiverBundle\File 一起使用,这看起来很奇怪。还要手动删除 var/cache/*(c:c 并不总是与元数据或其他缓存插件结合使用 - 还要确保你重新启动了 fpm)
    • @Vinc 如果您碰巧覆盖了需要更新依赖项等的类,因此您应该始终使用 REFACTOR 而不是删除。 Rufinus 可以请你决定你是想说德语还是 inklisch :D
    猜你喜欢
    • 2017-07-02
    • 2018-04-11
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2018-05-16
    • 2014-12-11
    相关资源
    最近更新 更多