Php 使用Symfony 4配置Sonata管理包
我正在尝试安装Sonata管理包,以便轻松生成各种实体的管理接口。我一直在关注官方文档(),但在创建第一个简单的管理员时,我遇到了一个无法解决的问题 我的实体是:Php 使用Symfony 4配置Sonata管理包,php,symfony,sonata-admin,sonata,Php,Symfony,Sonata Admin,Sonata,我正在尝试安装Sonata管理包,以便轻松生成各种实体的管理接口。我一直在关注官方文档(),但在创建第一个简单的管理员时,我遇到了一个无法解决的问题 我的实体是: <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Gedmo\Timestampable\Traits\TimestampableEntity; /** * @OR
<?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;
}
}
在我的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)}}
上得到一个错误“数组到字符串转换”。如果我注释掉向这些过滤器添加name属性的行,列表页面呈现良好,但是如果我随后进入form(creation/edition)视图,我会在另一个元素上得到相同的错误,因此我猜问题更严重
这可能是与Symfony 4.x的兼容性问题吗
提前谢谢 问题来自使用Misd电话号码包()
我还没有弄清楚原因,如果我找到了错误的原因或解决方案,我会编辑这个;) 我遇到了同样的问题。我想这是在Symfony3.4中发生的,在那里,他们引入了与bundle提供的小部件同名的新小部件 实际上,我不知道如何在没有BC中断的情况下解决它。到目前为止,我已经使用了一个解决方案,我修补PhoneNumberBundle并将tel_小部件重命名为misd_tel_小部件 这是我的补丁-。我正在使用在composer文件中应用修补程序 修补程序代码:
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') }}>
diff--git a/Form/Type/PhoneNumberType.php b/Form/Type/PhoneNumberType.php
索引58b67bf..9c31add 100644
---a/Form/Type/PhoneNumberType.php
+++b/Form/Type/PhoneNumberType.php
@@-183,6+183,6@@class PhoneNumberType扩展了AbstractType
*/
公共函数getBlockPrefix()
{
-返回“电话”;
+返回“misd_tel”;
}
}
diff--git a/Resources/views/Form/tel.html.twig b/Resources/views/Form/tel.html.twig
索引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为常量('Misd\\PhoneNumberBundle\\Form\\Type\\PhoneNumberType::widget\u COUNTRY\u CHOICE')%}
{{-form_小部件(form.country)-}
diff--git a/Resources/views/Form/tel_bootstrap.html.twig b/Resources/views/Form/tel_bootstrap.html.twig
索引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为常量('Misd\\PhoneNumberBundle\\Form\\Type\\PhoneNumberType::widget\u COUNTRY\u CHOICE')%}
{%set attr=attr | merge({class:(attr.class | default(“”)~'form inline')| trim})%}
一切正常,您能提供堆栈跟踪吗?您好,谢谢您的回答。下面是Symfony:ErrorException:Haha返回的堆栈跟踪,深入研究这个问题,似乎问题实际上来自另一个库。我要进一步调查!是的,显然问题是因为我使用了一个特殊的软件包来格式化电话号码。它在连电话号码都没有的实体上失败了,但是。。。。谢谢你的外表;)很高兴听到你的文件看起来不错,我有点困惑。如果您使用slack,您可以在#sonata频道上找到sonata的支持。谢谢,我将尝试一下!:)我还必须重写bundle的PhoneNumberType和DataTransformers,以消除一些错误(初始化表单时,Sonata将null值转换了两次,导致它失败)。但是你的解决方案让我走上了正轨,非常感谢!
admin_area:
resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
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') }}>