首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关系字段和Vich在Easyadmin上找不到映射

关系字段和Vich在Easyadmin上找不到映射
EN

Stack Overflow用户
提问于 2022-04-24 11:12:07
回答 1查看 482关注 0票数 0

我希望重构一些代码以避免重复;) Bundles使用:

6.0.7

  • vich/uploader-bundle 1.19

  • easycorp/easyadmin-bundle 4.1.1
  • symfony

我有一个内容实体,其中定义了所有的内容类型。MyImage属性是与图像实用实体的OneToOne关系,以避免重复代码。

当我试图显示页的编辑(或创建)页时,会引发以下错误:未找到字段“myImage_image”的映射。

这是有问题的文件。

内容实体:

代码语言:javascript
运行
复制
<?php

namespace App\Entity\Content;

use App\Entity\Media\Image;
use App\Repository\Content\ContentRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[ORM\Entity(repositoryClass: ContentRepository::class)]
#[ORM\Table(name: 'content')]
#[ORM\InheritanceType('JOINED')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap([
    'page' => Page::class,
    // ...
])]
#[Vich\Uploadable]
abstract class Content
{
    use SoftDeleteableEntity;
    use TimestampableEntity;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    // ...

    #[ORM\OneToOne(targetEntity: Image::class, cascade: ['persist', 'remove'])]
    private Image $myImage;

    public function __construct()
    {
        $this->myImage = new Image();
    }
    
    // ...

    public function getMyImage(): Image
    {
        return $this->myImage;
    }

    public function setMyImage(Image $myImage): self
    {
        $this->myImage = $myImage;

        return $this;
    }
}

图像实体:

代码语言:javascript
运行
复制
<?php

namespace App\Entity\Media;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\Timestampable;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[ORM\Entity]
#[Vich\Uploadable]
class Image
{
    use Timestampable;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id;

    #[Vich\UploadableField(mapping: 'content', fileNameProperty: 'imageName')]
    private ?File $image;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private ?string $imageName;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param  int|null  $id
     */
    public function setId(?int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return File|null
     */
    public function getImage(): ?File
    {
        return $this->image;
    }

    /**
     * @param $image
     *
     * @return $this
     */
    public function setImage($image): self
    {
        // 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');
        }

        return $this;
    }

    /**
     * @return string|null
     */
    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    /**
     * @param  string|null  $imageName
     *
     * @return $this
     */
    public function setImageName(?string $imageName): self
    {
        $this->imageName = $imageName;

        return $this;
    }
}

Vich配置:

代码语言:javascript
运行
复制
vich_uploader:
    db_driver: orm
    metadata:
        type: attribute
    mappings:
        content:
            uri_prefix: /uploads/content
            upload_destination: '%kernel.project_dir%/public/uploads/content'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
            directory_namer:
                service: Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer
                options:
                    date_time_format: 'Y' # will create directory "2018/23/09" for curent date "2018-09-23"
                    date_time_property: createdAt # see above example
            inject_on_load: false
            delete_on_update: true
            delete_on_remove: true

我肯定这是个愚蠢的错误,但我似乎听不懂。

(预先谢谢:)

编辑:

CRUD主计长:

代码语言:javascript
运行
复制
<?php

namespace App\Controller\Admin\Content;

use App\Entity\Content\Page;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Vich\UploaderBundle\Form\Type\VichImageType;

class PageCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Page::class;
    }

    public function configureFields(string $pageName): iterable
    {
        // ...

        yield Field::new('myImage.image')->setFormType(VichImageType::class);

            // ...
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-04-25 05:55:03

我创建了一个ImageType并在Controller中使用了它:

代码语言:javascript
运行
复制
// ...
     yield Field::new('myImage.image', false)->setFormType(ImageType::class);
// ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71987834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档