尝试删除文件时如何修复错误

时间:2019-04-19 15:31:18

标签: symfony vichuploaderbundle easyadmin

我正在创建一个网站,其中显示了我所做的项目。因此,当我添加一个新项目时,我不想自己修改数据库。因此,使用捆绑包easyAdmin,我创建了一个管理表单来管理它们。为了上传文件,我使用的是捆绑包vichuploader。到目前为止,没有问题,但是当我要删除该项目或修改文件时,出现此错误“在属性路径“名称”中给出了“字符串”,“ NULL”类型的预期参数。”

我什至不知道错误来自哪里。

包含文件的我的实体项目:

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProjectRepository")
 * @ApiResource
 * @Vich\Uploadable
 */
class Project
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @Vich\UploadableField(mapping="projects", fileNameProperty="name")
     * @var File
     */
    private $projectFile;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $url;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updateAt;

    public function __construct()
    {
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName(string $name)
    {
        $this->name = $name;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(string $url): self
    {
        $this->url = $url;

        return $this;
    }

    public function __toString()
    {
        return $this->nom;
    }

    public function getProjectFile()
    {
        return $this->projectFile;
    }

    public function setProjectFile(File $projectFile = null)
    {
        $this->projectFile = $projectFile;

        if ($projectFile) {
            $this->updateAt = new DateTime('now');
            $this->url = '%app.path.projects%' . $this->name;
        }
    }

    public function getUpdateAt(): ?\DateTimeInterface
    {
        return $this->updateAt;
    }

    public function setUpdateAt(\DateTimeInterface $updateAt): self
    {
        $this->updateAt = $updateAt;

        return $this;
    }


}

我的easy_admin.yaml文件:

easy_admin:
    entities:
        # List the entity class name you want to manage
        Project:
            class: App\Entity\Project
            form:
                fields:
                    - { property: 'name' }
                    - { property: 'projectFile', type: 'file' }

我的services.yaml文件:

parameters:
    locale: 'fr'
    app.path.projects: /projects

我的vich_uploader.yaml文件:

vich_uploader:
    db_driver: orm

    mappings:
        projects:
            uri_prefix: '%app.path.projects%'
            upload_destination: '%kernel.project_dir%/public%app.path.projects%'

这是我遇到的错误:

InvalidArgumentException
Symfony\Component\PropertyAccess\Exception\InvalidArgumentException:
Expected argument of type "string", "NULL" given at property path "name".

  at vendor/symfony/property-access/PropertyAccessor.php:173
  at Symfony\Component\PropertyAccess\PropertyAccessor::throwInvalidArgumentException('Argument 1 passed to App\\Entity\\Project::setName() must be of the type string, null given, called in /home/simon/Documents/Cours/Framework/TEA/vendor/symfony/property-access/PropertyAccessor.php on line 522', array(array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/property-access/PropertyAccessor.php', 'line' => 522, 'function' => 'setName', 'class' => 'App\\Entity\\Project', 'type' => '->', 'args' => array(null)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/property-access/PropertyAccessor.php', 'line' => 137, 'function' => 'writeProperty', 'class' => 'Symfony\\Component\\PropertyAccess\\PropertyAccessor', 'type' => '->', 'args' => array(array(object(Project), object(Project)), 'name', null)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/vich/uploader-bundle/Mapping/PropertyMapping.php', 'line' => 192, 'function' => 'setValue', 'class' => 'Symfony\\Component\\PropertyAccess\\PropertyAccessor', 'type' => '->', 'args' => array(object(Project), object(PropertyPath), null)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/vich/uploader-bundle/Mapping/PropertyMapping.php', 'line' => 136, 'function' => 'writeProperty', 'class' => 'Vich\\UploaderBundle\\Mapping\\PropertyMapping', 'type' => '->', 'args' => array(object(Project), 'name', null)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/vich/uploader-bundle/Handler/UploadHandler.php', 'line' => 100, 'function' => 'erase', 'class' => 'Vich\\UploaderBundle\\Mapping\\PropertyMapping', 'type' => '->', 'args' => array(object(Project))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/vich/uploader-bundle/EventListener/Doctrine/RemoveListener.php', 'line' => 58, 'function' => 'remove', 'class' => 'Vich\\UploaderBundle\\Handler\\UploadHandler', 'type' => '->', 'args' => array(object(Project), 'projectFile')), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/doctrine-bridge/ContainerAwareEventManager.php', 'line' => 61, 'function' => 'postRemove', 'class' => 'Vich\\UploaderBundle\\EventListener\\Doctrine\\RemoveListener', 'type' => '->', 'args' => array(object(LifecycleEventArgs))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php', 'line' => 117, 'function' => 'dispatchEvent', 'class' => 'Symfony\\Bridge\\Doctrine\\ContainerAwareEventManager', 'type' => '->', 'args' => array('postRemove', object(LifecycleEventArgs))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', 'line' => 1203, 'function' => 'invoke', 'class' => 'Doctrine\\ORM\\Event\\ListenersInvoker', 'type' => '->', 'args' => array(object(ClassMetadata), 'postRemove', object(Project), object(LifecycleEventArgs), 4)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', 'line' => 409, 'function' => 'executeDeletions', 'class' => 'Doctrine\\ORM\\UnitOfWork', 'type' => '->', 'args' => array(object(ClassMetadata))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php', 'line' => 359, 'function' => 'commit', 'class' => 'Doctrine\\ORM\\UnitOfWork', 'type' => '->', 'args' => array(null)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php', 'line' => 507, 'function' => 'flush', 'class' => 'Doctrine\\ORM\\EntityManager', 'type' => '->', 'args' => array()), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php', 'line' => 759, 'function' => 'removeEntity', 'class' => 'EasyCorp\\Bundle\\EasyAdminBundle\\Controller\\EasyAdminController', 'type' => '->', 'args' => array(object(Project), object(Form))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php', 'line' => 332, 'function' => 'executeDynamicMethod', 'class' => 'EasyCorp\\Bundle\\EasyAdminBundle\\Controller\\EasyAdminController', 'type' => '->', 'args' => array('remove<EntityName>Entity', array(object(Project), object(Form)))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php', 'line' => 759, 'function' => 'deleteAction', 'class' => 'EasyCorp\\Bundle\\EasyAdminBundle\\Controller\\EasyAdminController', 'type' => '->', 'args' => array()), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php', 'line' => 70, 'function' => 'executeDynamicMethod', 'class' => 'EasyCorp\\Bundle\\EasyAdminBundle\\Controller\\EasyAdminController', 'type' => '->', 'args' => array('delete<EntityName>Action')), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/http-kernel/HttpKernel.php', 'line' => 150, 'function' => 'indexAction', 'class' => 'EasyCorp\\Bundle\\EasyAdminBundle\\Controller\\EasyAdminController', 'type' => '->', 'args' => array(object(Request))), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/http-kernel/HttpKernel.php', 'line' => 67, 'function' => 'handleRaw', 'class' => 'Symfony\\Component\\HttpKernel\\HttpKernel', 'type' => '->', 'args' => array(object(Request), 1)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/vendor/symfony/http-kernel/Kernel.php', 'line' => 198, 'function' => 'handle', 'class' => 'Symfony\\Component\\HttpKernel\\HttpKernel', 'type' => '->', 'args' => array(object(Request), 1, true)), array('file' => '/home/simon/Documents/Cours/Framework/TEA/public/index.php', 'line' => 25, 'function' => 'handle', 'class' => 'Symfony\\Component\\HttpKernel\\Kernel', 'type' => '->', 'args' => array(object(Request)))), 0, object(PropertyPath))
     (vendor/symfony/property-access/PropertyAccessor.php:153)
  at Symfony\Component\PropertyAccess\PropertyAccessor->setValue(object(Project), object(PropertyPath), null)
     (vendor/vich/uploader-bundle/Mapping/PropertyMapping.php:192)
  at Vich\UploaderBundle\Mapping\PropertyMapping->writeProperty(object(Project), 'name', null)
     (vendor/vich/uploader-bundle/Mapping/PropertyMapping.php:136)
  at Vich\UploaderBundle\Mapping\PropertyMapping->erase(object(Project))
     (vendor/vich/uploader-bundle/Handler/UploadHandler.php:100)
  at Vich\UploaderBundle\Handler\UploadHandler->remove(object(Project), 'projectFile')
     (vendor/vich/uploader-bundle/EventListener/Doctrine/RemoveListener.php:58)
  at Vich\UploaderBundle\EventListener\Doctrine\RemoveListener->postRemove(object(LifecycleEventArgs))
     (vendor/symfony/doctrine-bridge/ContainerAwareEventManager.php:61)
  at Symfony\Bridge\Doctrine\ContainerAwareEventManager->dispatchEvent('postRemove', object(LifecycleEventArgs))
     (vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php:117)
  at Doctrine\ORM\Event\ListenersInvoker->invoke(object(ClassMetadata), 'postRemove', object(Project), object(LifecycleEventArgs), 4)
     (vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1203)
  at Doctrine\ORM\UnitOfWork->executeDeletions(object(ClassMetadata))
     (vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:409)
  at Doctrine\ORM\UnitOfWork->commit(null)
     (vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:359)
  at Doctrine\ORM\EntityManager->flush()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:507)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->removeEntity(object(Project), object(Form))
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:759)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->executeDynamicMethod('remove<EntityName>Entity', array(object(Project), object(Form)))
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:332)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->deleteAction()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:759)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->executeDynamicMethod('delete<EntityName>Action')
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:70)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->indexAction(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:150)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:67)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:198)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25)

3 个答案:

答案 0 :(得分:0)

您的项目对象有名称吗?听起来像$ project-> getName()为空。

答案 1 :(得分:0)

 /**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $name;

您可以尝试为“名称”添加“ nullable = true”

答案 2 :(得分:0)

如果您不将public function setName(string $name)更改为public function setName(string $name = null),则

nullable = true不会有帮助,这样做甚至可以删除nullable = true