无法确定类“App\Entity\XXXX”中属性“image”的访问类型。 Symfony 4 - EasyAdmin 3.2 - VichUploader

Posted

技术标签:

【中文标题】无法确定类“App\\Entity\\XXXX”中属性“image”的访问类型。 Symfony 4 - EasyAdmin 3.2 - VichUploader【英文标题】:Could not determine access type for property "image" in class "App\Entity\XXXX". Symfony 4 - EasyAdmin 3.2 - VichUploader无法确定类“App\Entity\XXXX”中属性“image”的访问类型。 Symfony 4 - EasyAdmin 3.2 - VichUploader 【发布时间】:2021-05-07 23:54:22 【问题描述】:

在这里努力尝试将 VichImageUploader 集成到我的 EasyAdmin 3.2 中。

这个版本的 EasyAdmin 让我们可以创建自定义字段,效果很好。

就我而言,我只是尝试上传 1 张图片并将其推送到我的数据库中。我设置了我的 Easy Admin 仪表板,然后按照以下步骤操作: https://symfony.com/doc/2.x/bundles/EasyAdminBundle/integration/vichuploaderbundle.html 在我的 CrudController 中补充我的 configureFields 函数。 与文档中一样,我使用 seter 和 geters 将 imageFile 字段连接到图像字段 althogeter。 在我的 CrudController 中,我使用了我的自定义字段,因为它似乎是在这个版本的 easyadmin 中进行图像上传的唯一方法。

我的 CrudController

namespace App\Controller\Admin;



use App\Entity\ButtonPlant;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\UrlField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use Vich\UploaderBundle\Form\Type\VichImageType;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\VichImageField;
class ButtonPlantCrudController extends AbstractCrudController

public static function getEntityFqcn(): string

    return ButtonPlant::class;


public function configureFields(string $pageName): iterable

    $imageFile = VichImageField::new('imageFile')->setFormType(VichImageType::class);
    $image = ImageField::new('image')->setBasePath('/uploads/images');

    $fields = [
        TextField::new('content', 'Contenu'),
        /* CollectionField::new('image')
        ->setEntryType(ImageType::class)
        ->setUploadDir('public\uploads\images\buttonplants'),
        ImageField::new('imageFile')->setFormType(VichImageType::class), */
        AssociationField::new('stepId', 'Etape'),
        AssociationField::new('nextStepId', 'Prochaine Etape' ),
        AssociationField::new('finalSheetId', 'Fiche Final'),
    ];

    if ($pageName == Crud::PAGE_INDEX || $pageName == Crud::PAGE_DETAIL) 
        $fields[] = $image;
     else 
        $fields[] = $imageFile;
    
    return $fields;



我的实体控制器

namespace App\Entity;
use App\Repository\ButtonPlantRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use DateTime;

/**
* @ORM\Entity(repositoryClass=ButtonPlantRepository::class)
* @Vich\Uploadable
*/
class ButtonPlant
 
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
private $id;

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

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

/**
 * @Vich\UploadableField(mapping="buttonplant_images", fileNameProperty="image")
 * @var File
 */
private $imageFile;


/**
 * @ORM\OneToOne(targetEntity=FinalSheet::class, cascade="persist", "remove")
 */
private $finalSheetId;

/**
 * @ORM\ManyToOne(targetEntity=CoursePlant::class, inversedBy="buttonPlants")
 * @ORM\JoinColumn(nullable=false)
 */
private $stepId;

/**
 * @ORM\OneToOne(targetEntity=CoursePlant::class, cascade="persist", "remove")
 */
private $nextStepId;

/**
 * @ORM\Column(type="datetime", nullable=true)
 * @var \DateTime
 */
private $updatedAt;


public function getId(): ?int

    return $this->id;


public function getContent(): ?string

    return $this->content;


public function setContent(string $content): self

    $this->content = $content;

    return $this;


public function getImage(): ?string

    return $this->image;


public function setIamge(string $image): self

    $this->image = $image;

    return $this;


public function setImageFile(File $image = null)

    $this->imageFile = $image;

    // VERY IMPORTANT:
    // 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
    if ($image) 
        // if 'updatedAt' is not defined in your entity, use another property
        $this->updatedAt = new \DateTime('now');
    


public function getImageFile()

    return $this->imageFile;


public function getFinalSheetId(): ?FinalSheet

    return $this->finalSheetId;


public function setFinalSheetId(?FinalSheet $finalSheetId): self

    $this->finalSheetId = $finalSheetId;

    return $this;


public function getStepId(): ?CoursePlant

    return $this->stepId;


public function setStepId(?CoursePlant $stepId): self

    $this->stepId = $stepId;

    return $this;


public function getNextStepId(): ?CoursePlant

    return $this->nextStepId;


public function setNextStepId(?CoursePlant $nextStepId): self

    $this->nextStepId = $nextStepId;

    return $this;


public function getUpdatedAt(): ?\DateTimeInterface

    return $this->updatedAt;


public function setUpdatedAt(?\DateTimeInterface $updatedAt): self

    $this->updatedAt = $updatedAt;

    return $this;



我的自定义字段

namespace EasyCorp\Bundle\EasyAdminBundle\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Vich\UploaderBundle\Form\Type\VichImageType;

class VichImageField implements FieldInterface

use FieldTrait;

public static function new(string $propertyName, ?string $label = null)

    return (new self())
        ->setProperty($propertyName)
        ->setTemplatePath('')
        ->setLabel($label)
        ->setFormType(VichImageType::class);



我的错误是

无法确定“App\Entity\ButtonPlant”类中属性“image”的访问类型。

提前感谢您的帮助

【问题讨论】:

【参考方案1】:

我解决了删除“图像”字段并重新创建它的问题,但这次允许为空。 希望它对任何人都有用

【讨论】:

以上是关于无法确定类“App\Entity\XXXX”中属性“image”的访问类型。 Symfony 4 - EasyAdmin 3.2 - VichUploader的主要内容,如果未能解决你的问题,请参考以下文章

如何找到持有类或 id 的属性?

JAVA抽象类和抽象方法(abstract)

如何修复实体框架中的“无法确定导航属性表示的关系”错误

无法读取未定义的属性,Typescript

自动属性无法在 C# 实例构造函数中初始化

Java 基础:抽象类与接口