无法确定 class "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

在这里挣扎尝试将 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);
}

}

而我的错误是

无法确定 属性“图片”在 class“App\Entity\ButtonPlant”中的访问类型。

提前感谢您的帮助

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