微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony/Sonata ModelType 无法正常工作

如何解决Symfony/Sonata ModelType 无法正常工作

我正在使用 Sonata Admin Bundle 对实体进行 crud 操作。

该实体与自身存在单对多关系。

表单正确显示,但我尝试添加到实体的项目没有被添加

当我手动将一个实体链接数据库中的另一个实体时,它在编辑表单上正确显示,但仍然无法更改。

这是我的代码

NavBaradmin.PHP :

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->add('text',TextType::class)
        ->add('link',TextType::class)
        ->add('children',ModelType::class,[
        'class' => NavItem::class,'property' => 'text','multiple' => true
    ]);
}

services.yaml :

admin.navbar:
    class: App\Admin\NavBaradmin
    arguments: [ ~,App\Entity\NavItem,App\Controller\NavBaradminController,~ ]
    tags:
        - { name: sonata.admin,manager_type: orm,label: Nav bar }

NavItem.PHP :

namespace App\Entity;

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


class NavItem
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\ManyToOne(targetEntity=NavItem::class,inversedBy="children")
     */
    private $parent;

    /**
     * @ORM\OnetoMany(targetEntity=NavItem::class,mappedBy="parent")
     */
    private $children;


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

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

    public function getText(): ?string
    {
        return $this->text;
    }

    public function setText(string $text): self
    {
        $this->text = $text;

        return $this;
    }

    public function getLink(): ?string
    {
        return $this->link;
    }

    public function setLink(string $link): self
    {
        $this->link = $link;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|self[]
     */
    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function addChild(self $child): self
    {
        if (!$this->children->contains($child)) {
            $this->children[] = $child;
            $child->setParent($this);
        }

        return $this;
    }

    public function removeChild(self $child): self
    {
        if ($this->children->removeElement($child)) {
            // set the owning side to null (unless already changed)
            if ($child->getParent() === $this) {
                $child->setParent(null);
            }
        }

        return $this;
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。