簡體   English   中英

嘗試刪除文件時如何修復錯誤

[英]How to fix error when trying to delete file

我正在創建一個網站來展示我所做的項目。 所以當我添加一個新項目時,我不想自己修改我的數據庫。 因此,使用捆綁包 easyAdmin,我創建了一個管理表單來管理它們。 為了上傳文件,我使用了 bundle 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)

您的項目對象有名稱嗎? 聽起來像$ project-> getName()是null。

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

您可以嘗試為“名稱”添加“nullable = true”

如果不將public function setName(string $name)更改為public function setName(string $name = null) ,則nullable = true public function setName(string $name = null) ,執行此操作甚至可以刪除nullable = true

VichUploaderBundle將始終在刪除時將null值傳遞給 setter,並且您無法更改此行為。

我看到以下修復方法:

使$name空。

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

或者,如果您不想更改數據庫,您可以簡單地更改您的 setter,如下所示:

public function setName(?string $name)
{
    if ($name !== null) {
        $this->name = $name;
    }

    return $this;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM