简体   繁体   English

数组到字符串转换。 symfony2.7

[英]Array to string conversion. symfony2.7

I have a dropdown with a list of entities + icon next to the entity. 我有一个下拉列表,其中包含实体旁边的实体+图标列表。 but when I submit my form I got this error: 但是当我提交表单时,我收到了这个错误:

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in src\\FLY\\BookingsBundle\\Resources\\views\\Post\\show.html.twig at line 38. 在第38行的src \\ FLY \\ BookingsBundle \\ Resources \\ views \\ Post \\ show.html.twig中呈现模板(“通知:数组到字符串转换”)期间抛出异常。

CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in "C:\\xampp\\htdocs\\Symfony\\src\\FLY\\BookingsBundle/Resources/views/Post/show.html.twig" at line 38." CRITICAL - 未捕获的PHP异常Twig_Error_Runtime:“在C:\\ xampp \\ htdocs \\ Symfony \\ src \\ FLY \\ BookingsBundle / Resources / views /中呈现模板(”通知:数组到字符串转换“)期间抛出异常发布/ show.html.twig“在第38行。” at C:\\xampp\\htdocs\\Symfony\\app\\cache\\dev\\classes.php line 4795 . 在C:\\ xampp \\ htdocs \\ Symfony \\ app \\ cache \\ dev \\ classes.php第4795行。

class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var array
     *
     * @ORM\Column(name="compagny", type="array")
     */
    private $compagny;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set compagny
     *
     * @param array $compagny
     * @return Post
     */
    public function setCompagny($compagny)
    {
        $this->compagny = $compagny;

        return $this;
    }

    /**
     * Get compagny
     *
     * @return array
     */
    public function getCompagny()
    {
        return $this->compagny;
    }
}

.

 ->add('compagny', 'choice', [
                    'required' => true,
                    'multiple' => true,
                    'label' => 'Ex:Emirates airways',
                    'attr' => [
                        'class' => 'form-control myDropdown',
                        'placeholder' => 'Ex:Emirates airways',
                    ]])

.

{% extends '::base.html.twig' %}

{% block body -%}
    <h1>Post</h1>

    <table class="record_properties">
        <tbody>
            <tr>
                <th>Id</th>
                <td>{{ entity.id }}</td>
            </tr>
            <tr>
                <th>Departure</th>
                <td>{{ entity.airport }}</td>
            </tr>
            <tr>
                <th>Arrival</th>
                <td>{{ entity.airport1 }}</td>
            </tr>
            <tr>
                <th>Departuredate</th>
                <td>{{ entity.departuredate|date('Y-m-d H:i:s') }}</td>
            </tr>
            <tr>
                <th>Arrivaldate</th>
                <td>{{ entity.arrivaldate|date('Y-m-d H:i:s') }}</td>
            </tr>

            <tr>
                <th>Compagny</th>
                <td>{{ entity.compagny }}</td>
            </tr>
        </tbody>
    </table>

        <ul class="record_actions">
    <li>
        <a href="{{ path('post') }}">
            Back to the list
        </a>
    </li>
    <li>
        <a href="{{ path('post_edit', { 'id': entity.id }) }}">
            Edit
        </a>
    </li>
    <li>{{ form(delete_form) }}</li>
</ul>
{% endblock %}

new.html.twig new.html.twig

<div class="col-md-2">
    <h4 class="title">Compagny</h4>
    <div class="form-group form-group-lg form-group-icon-left">
        <i class="fa fa-plane input-icon"></i>
        <label>Airlines</label>
        {{ form_widget(form.compagny, { 'attr': {'class': 'form-control myDropdown',} }) }}
        {{ form_errors(form.compagny) }}
    </div>
</div>

Your $compagny property of Post is an array, just as you defined in the annotation: Post的$ compagny属性是一个数组,正如您在注释中定义的那样:

/**
 * @var array
 *
 * @ORM\Column(name="compagny", type="array")
 */

read doctrine documentation, this array will be serialized before storing into database. 阅读学说文档,该数组将在存储到数据库之前被序列化。

And you can't render it directly in twig. 你不能直接在树枝上渲染它。

You need use for to display the items in the array one by one. 你需要使用for一个来显示阵列中的一个项目。

<ul>
{% for item in entity.compagny %}
    <li>{{ item }}</li>
{% endfor %}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM