简体   繁体   中英

How to fix error when trying to delete file

I'm creating a website which show project I made. So when I'm adding a new project, I don't want to modify my database myself. So with the bundle easyAdmin, I create an admin form to manage them. For upload a file I'm using the bundle vichuploader. Until now no problem, but when I want to delete this project or to modify the file I got this error 'Expected argument of type "string", "NULL" given at property path "name".'

I don't even know where the error come from.

My entity Project which contain the file :

/**
 * @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;
    }


}

My easy_admin.yaml file :

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' }

My services.yaml file :

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

My vich_uploader.yaml file :

vich_uploader:
    db_driver: orm

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

Here the error I got :

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)

Did your project object has a name? Sounds like $project->getName() is null.

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

You can try to add "nullable=true" for "name"

如果不将public function setName(string $name)更改为public function setName(string $name = null) ,则nullable = true public function setName(string $name = null) ,执行此操作甚至可以删除nullable = true

The VichUploaderBundle will always pass a null value to the setter on deletion, and you cannot change this behavior.

I see the following ways to fix it:

Make the $name nullable.

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

Or, if you don't want to make changes to the database, you can simply change your setter like this:

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

    return $this;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM