简体   繁体   English

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

Strugling here trygin to integrate VichImageUploader into my EasyAdmin 3.2.在这里努力尝试将 VichImageUploader 集成到我的 EasyAdmin 3.2 中。

This version of EasyAdmin is letting us create custom Fields which works just fine.这个版本的 EasyAdmin 让我们可以创建自定义字段,效果很好。

In my case I am only trying to upload 1 image and push it into my DB.就我而言,我只是想上传 1 张图片并将其推送到我的数据库中。 I set up my Easy Admin dashboard and just followed: https://symfony.com/doc/2.x/bundles/EasyAdminBundle/integration/vichuploaderbundle.html to hydrate my configureFields function inside my CrudController. I set up my Easy Admin dashboard and just followed: https://symfony.com/doc/2.x/bundles/EasyAdminBundle/integration/vichuploaderbundle.html to hydrate my configureFields function inside my CrudController. As in the docs, I made a imageFile field joint to a image field althogeter with seters and geters.与文档中一样,我使用 seter 和 geters 将 imageFile 字段与图像字段 althogeter 连接起来。 Inside my CrudController I use my custom field because it seems its the only way to do image uploads in this version of easyadmin.在我的 CrudController 中,我使用了我的自定义字段,因为它似乎是在这个版本的 easyadmin 中进行图像上传的唯一方法。

My CrudController我的 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;


}

My Entity Controller我的实体 Controller

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

}

My custom Field我的自定义字段

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);
}

}

And my error is我的错误是

Could not determine access type for property "image" in class "App\Entity\ButtonPlant".无法确定 class“App\Entity\ButtonPlant”中属性“image”的访问类型。

Thanks in advance for any help提前感谢您的帮助

I solved my problem deleting the field "image" and creating it back but this time is allowed to be null.我解决了删除“图像”字段并重新创建它的问题,但这次允许为 null。 Hopefully it can be useful for anyone希望它对任何人都有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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