使用Symfony 4配置Sonata Admin Bundle

时间:2018-04-10 09:53:09

标签: php symfony sonata-admin sonata

我正在尝试安装Sonata Admin Bundle来生成各种实体的管理界面。我一直在关注官方文档(https://symfony.com/doc/master/bundles/SonataAdminBundle/getting_started/creating_an_admin.html),但我遇到了一个在创建我的第一个简单管理员时无法弄清楚的问题。

我的实体是:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\SectorRepository")
 */
class Sector
{
    use TimestampableEntity;

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

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

    /**
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(type="string", length=128)
     */
    private $slug;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Company", mappedBy="sector", cascade = {"persist"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     */
    private $companies;

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

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

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

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

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getCompanies(): array
    {
        return $this->companies;
    }

    public function setCompanies($companies): self
    {
        $this->companies = $companies;

        return $this;
    }
}

我的Admin类看起来像这样:

<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class SectorAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('name', TextType::class);
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('name');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('name');
    }
}

我已添加此配置:

services:
    admin.sector:
        class: App\Admin\SectorAdmin
        arguments: [ ~, App\Entity\Sector, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Sector}
    public: true

在我的sonata管理路线文件中:

admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

正如我从文档中了解到管理员注册的最低配置。

然而,当我运行我的代码时,我在datagrid过滤器的{{ form_errors(form) }}上收到错误“Array to string conversion”。如果我注释掉将name属性添加到这些过滤器的行,列表页面呈现正常,但如果我然后输入表单(创建/编辑)视图,我在另一个元素上得到相同的错误,所以我猜问题更深。< / p>

这可能是与Symfony 4.x兼容的问题吗?

先谢谢!

2 个答案:

答案 0 :(得分:1)

问题来自使用Misd电话号码包(https://github.com/misd-service-development/phone-number-bundle)。

我仍然没有想出原因,如果我找到错误的原因或解决方法,我会编辑它;)

答案 1 :(得分:0)

我遇到了同样的问题。它发生在我想到的symfony 3.4中,他们引入了新的小部件,其名称与bundle提供的小部件相同。

实际上我不知道如何在没有BC休息的情况下解决它。到目前为止,我已经使用了解决方案,我修补了PhoneNumberBundle并将tel_widget重命名为misd_tel_widget。

这是我的补丁 - https://pastebin.com/CnjXB1bi。我正在使用https://github.com/cweagans/composer-patches在作曲家文件中应用补丁。

补丁码:

diff --git a/Form/Type/PhoneNumberType.php b/Form/Type/PhoneNumberType.php
index 58b67bf..9c31add 100644
--- a/Form/Type/PhoneNumberType.php
+++ b/Form/Type/PhoneNumberType.php
@@ -183,6 +183,6 @@ class PhoneNumberType extends AbstractType
      */
     public function getBlockPrefix()
     {
-        return 'tel';
+        return 'misd_tel';
     }
 }
diff --git a/Resources/views/Form/tel.html.twig b/Resources/views/Form/tel.html.twig
index 1fdfed4..c8064e4 100644
--- a/Resources/views/Form/tel.html.twig
+++ b/Resources/views/Form/tel.html.twig
@@ -1,4 +1,4 @@
-{% block tel_widget -%}
+{% block misd_tel_widget -%}
     {% if widget is constant('Misd\\PhoneNumberBundle\\Form\\Type\\PhoneNumberType::WIDGET_COUNTRY_CHOICE') %}
         <div {{ block('widget_container_attributes') }}>
             {{- form_widget(form.country) -}}
diff --git a/Resources/views/Form/tel_bootstrap.html.twig b/Resources/views/Form/tel_bootstrap.html.twig
index 79f4748..7772df9 100644
--- a/Resources/views/Form/tel_bootstrap.html.twig
+++ b/Resources/views/Form/tel_bootstrap.html.twig
@@ -1,4 +1,4 @@
-{% block tel_widget -%}
+{% block misd_tel_widget -%}
     {% if widget is constant('Misd\\PhoneNumberBundle\\Form\\Type\\PhoneNumberType::WIDGET_COUNTRY_CHOICE') %}
         {% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) %}
         <div {{ block('widget_container_attributes') }}>