无法确定类“App\Entity\XXXX”中属性“image”的访问类型 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 函数。 与文档中一样,我使用 seters 和 geters 将 imageFile 字段连接到图像字段 althogeter。 在我的 CrudController 中,我使用我的自定义字段,因为它似乎是在这个版本的 easyadmin 中上传图片的唯一方法

我的 CrudController

[
    {
        "id": 3,"priority": 1,"action": {
            "type": "block"
        },"condition": {
            "urlFilter": "youtube.com"
        }
    }
]

我的实体控制器

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,"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;
}

}

我的错误

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

在此先感谢您的帮助

解决方法

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