簡體   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

在這里努力嘗試將 VichImageUploader 集成到我的 EasyAdmin 3.2 中。

這個版本的 EasyAdmin 讓我們可以創建自定義字段,效果很好。

就我而言,我只是想上傳 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. 與文檔中一樣,我使用 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;


}

我的實體 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;
}

}

我的自定義字段

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”中屬性“image”的訪問類型。

提前感謝您的幫助

我解決了刪除“圖像”字段並重新創建它的問題,但這次允許為 null。 希望它對任何人都有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM