【问题标题】:Symfony2 notice: Array to string conversion in Sonata AdminSymfony2 通知:Sonata Admin 中的数组到字符串转换
【发布时间】:2012-12-19 18:13:20
【问题描述】:

我在 Symfony 2.1.3 上使用 Sonata admin。现在尝试使用谷歌地图添加文本输入。

configureFormFields()函数中添加一行:

->add('coordinates', 'contentbundle_coordinates_map', array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

在服务中注册并为此创建模板:

{% block contentbundle_coordinates_map_widget %}
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>
    <script type="text/javascript" src="{{ asset('js/add.scripts.js') }}"></script>
    <input type="text" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
    <div id="add_map" class="map" style="width:500px;height:300px;"></div>
{% endblock %}

可以在管理内容添加页面中看到我的带有地图的字段,但是当我想提交数据时:

Notice: Array to string conversion in D:\my_vendor_folder\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103

如果我将 contentbundle_coordinates_map 更改为 null

->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

一切正常。

问题出在哪里?

更新

表单类型类:

namespace Map\ContentBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class CoordinatesMapType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'contentbundle_coordinates_map';
    }
}

【问题讨论】:

  • 我们可能需要能够看到contentbundle_coordinates_map 的表单类型。我的第一个猜测是它的getParent() 方法没有返回正确的类型。
  • @RobMasters 更新了表单类型的问题。你说得对,我什至没有 getParent 方法。你能告诉我它需要退货吗?请把它写成答案,这样我就可以正确标记了。

标签: php symfony twig symfony-2.1 sonata-admin


【解决方案1】:

您应该始终为自定义表单类型定义getParent 方法,以便继承该特定类型的逻辑。有关类型列表,请参阅 here

在这种情况下,您的自定义类型似乎应该返回文本,因此将以下内容添加到CoordinatesMapType

public function getParent()
{
    return 'text';
}

作为替代方案,如果您只需要自定义表单字段的呈现,那么您甚至不需要创建自己的自定义表单类型。见How to customize an Individual field。我认为这只有在您手动为表单命名时才有可能。 (假设它的名字是“内容”)

{# This is in the view where you're rendering the form #}

{% form_theme form _self %}

{% block _content_coordinates_widget %}
    <div class="text_widget">
        <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>
        <script type="text/javascript" src="{{ asset('js/add.scripts.js') }}"></script>
        <input type="text" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
        <div id="add_map" class="map" style="width:500px;height:300px;"></div>
    </div>
{% endblock %}

在这种情况下,您可以将类型指定为 null,如您的第二个示例中所示:

->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多