Как исправить ошибку при попытке удалить файл - PullRequest
0 голосов
/ 19 апреля 2019

Я создаю сайт, который показывает проект, который я сделал. Поэтому, когда я добавляю новый проект, я не хочу сам изменять свою базу данных. Поэтому с пакетом easyAdmin я создаю форму администратора для управления ими. Для загрузки файла я использую пакет vichuploader. До сих пор проблем не было, но когда я хочу удалить этот проект или изменить файл, я получил эту ошибку 'Ожидаемый аргумент типа "строка", "NULL", заданный в пути свойства "name".'

Я даже не знаю, откуда возникла ошибка.

Мой объект Project, содержащий файл:

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

Файл My 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)

Ответы [ 2 ]

0 голосов
/ 20 апреля 2019
 /**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $name;

Вы можете попробовать добавить «nullable = true» для «name»

0 голосов
/ 20 апреля 2019

У вашего объекта проекта есть имя?Похоже, $ project-> getName () имеет значение null.

...