【问题标题】:Symfony EasyAdmin 3.x ManyToMany error when adding : The Doctrine type of the .... field is "4", which is not supported by EasyAdmin yetSymfony EasyAdmin 3.x ManyToMany 添加时出现错误:...字段的 Doctrine 类型为“4”,EasyAdmin 尚不支持
【发布时间】:2020-10-27 12:45:20
【问题描述】:

我正在尝试使用 easyAdmin 3.x 在两个类之间建立一个简单的 ManyToMany 关系,当我尝试显示实体 CRUD 时,我经常遇到此错误:

“salles”字段的Doctrine类型为“4”,EasyAdmin尚不支持。

两个实体都存在函数__to string

public function __toString()
    {
        return $this->name;
    }

我的 CrudController:

namespace App\Controller\Admin;

use App\Entity\Batiment;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;


class BatimentCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Batiment::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            'name',
            'salles'

        ];
    }

}

easyadmin 3.x 不管理多对多关系吗?

是否有一种特殊的方式来管理和显示这些关系?

我发现了这个捆绑包,感谢您的帮助!

【问题讨论】:

  • 你会展示你的 Batiment 实体类吗?
  • 你能展示你的 batment 简单的管理配置(yaml)吗?
  • 你找到解决这个问题的方法了吗?

标签: symfony easyadmin


【解决方案1】:

在 EasyAdmin 3.x 中,为所有关联映射添加了一种新的专用类型。

请使用类似:

namespace App\Controller\Admin;
    
use App\Entity\Batiment;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
        
        
class BatimentCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Batiment::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            Field::new('id')->hideOnForm(),
            Field::new('name'),
            Field::new('Letter'),
            ImageField::new('image'),
            AssociationField::new('products')
        ];
    }
}

【讨论】:

  • AssociationField::new('...') 为我工作。谢谢;-)
  • AssociationField::new('products') 为我工作,谢谢!
【解决方案2】:

这是我的Batiment实体类的定义

    <?php

namespace App\Entity;

use App\Repository\BatimentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=BatimentRepository::class)
 */
class Batiment
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $Letter;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @ORM\OneToMany(targetEntity=Salle::class, mappedBy="batiment")
     */
    private $salles;

    public function __construct()
    {
        $this->salles = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getLetter(): ?string
    {
        return $this->Letter;
    }

    public function setLetter(?string $Letter): self
    {
        $this->Letter = $Letter;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(?string $image): self
    {
        $this->image = $image;

        return $this;
    }

    /**
     * @return Collection|Salle[]
     */
    public function getSalles(): Collection
    {
        return $this->salles;
    }

    public function addSalle(Salle $salle): self
    {
        if (!$this->salles->contains($salle)) {
            $this->salles[] = $salle;
            $salle->setBatiment($this);
        }

        return $this;
    }

    public function removeSalle(Salle $salle): self
    {
        if ($this->salles->contains($salle)) {
            $this->salles->removeElement($salle);
            // set the owning side to null (unless already changed)
            if ($salle->getBatiment() === $this) {
                $salle->setBatiment(null);
            }
        }

        return $this;
    }

    public function __toString()
    {
        return $this->name;
    }
}

【讨论】:

    【解决方案3】:

    关于batiment easy admin config (yaml),我没有特别的配置。

    我在easyadmin 3.x的文档中没有找到关于这个问题的一点。`

    使用 easyadmin documention 2.x 我尝试将这些参数放在 config/packages/easyadmin.yaml 中。

    没有成功!

    easy_admin:
        entities:
    #        # List the entity class name you want to manage
            - App\Entity\Batiment
            - App\Entity\DispositionSalle
            - App\Entity\Salle
    
    

    【讨论】:

    猜你喜欢
    • 2021-04-30
    • 2021-11-21
    • 2019-01-31
    • 1970-01-01
    • 2021-02-10
    • 2019-07-11
    • 2021-10-14
    • 2018-11-03
    • 2019-07-27
    相关资源
    最近更新 更多