使用 Symfony 4 配置 Sonata Admin Bundle
Configuring Sonata Admin Bundle with Symfony 4
我正在尝试安装 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;
}
}
我的管理员 class 看起来像这样:
<?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
在我的奏鸣曲管理路线文件中:
admin_area:
resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
据我从文档中了解到,这是让管理员注册的最低配置。
然而,当我 运行 我的代码时,我在数据网格过滤器的 {{ form_errors(form) }}
上收到错误 "Array to string conversion"。如果我注释掉向这些过滤器添加 name 属性的行,列表页面呈现正常,但如果我随后输入表单 (creation/edition) 视图,我在另一个元素上得到相同的错误,所以我想问题更深.
会不会是与 Symfony 的兼容性问题4.x?
提前致谢!
问题来自于使用 Misd Phone 号码包 (https://github.com/misd-service-development/phone-number-bundle)。
我还没有弄清楚为什么,如果我找到错误的原因或解决方法,我会编辑它;)
我运行同样的问题。我认为它发生在 symfony 3.4 中,他们引入了与 bundle 提供的同名的新小部件。
其实我不知道如何在没有BC中断的情况下解决它。到目前为止,我使用的解决方案是修补 PhoneNumberBundle 并将 tel_widget 重命名为 misd_tel_widget.
这是我的补丁 - https://pastebin.com/CnjXB1bi. I'm applying patch in composer file using 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') }}>
我正在尝试安装 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;
}
}
我的管理员 class 看起来像这样:
<?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
在我的奏鸣曲管理路线文件中:
admin_area:
resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
据我从文档中了解到,这是让管理员注册的最低配置。
然而,当我 运行 我的代码时,我在数据网格过滤器的 {{ form_errors(form) }}
上收到错误 "Array to string conversion"。如果我注释掉向这些过滤器添加 name 属性的行,列表页面呈现正常,但如果我随后输入表单 (creation/edition) 视图,我在另一个元素上得到相同的错误,所以我想问题更深.
会不会是与 Symfony 的兼容性问题4.x?
提前致谢!
问题来自于使用 Misd Phone 号码包 (https://github.com/misd-service-development/phone-number-bundle)。
我还没有弄清楚为什么,如果我找到错误的原因或解决方法,我会编辑它;)
我运行同样的问题。我认为它发生在 symfony 3.4 中,他们引入了与 bundle 提供的同名的新小部件。
其实我不知道如何在没有BC中断的情况下解决它。到目前为止,我使用的解决方案是修补 PhoneNumberBundle 并将 tel_widget 重命名为 misd_tel_widget.
这是我的补丁 - https://pastebin.com/CnjXB1bi. I'm applying patch in composer file using 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') }}>