4
votes

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.

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." at C:\xampp\htdocs\Symfony\app\cache\dev\classes.php line 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

<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>
1
You got this error in your TWIG file, but this is missing in your question. Please add it into your question. - KhorneHoly
@KhorneHoly i add my twig file in my question. - Sirius
I don't really understand what are you trying to do here, but the error is in this line {{ entity.compagny }}, compagny is an array defined in your entity, so it can't be just echo'ed by twig. Maybe Compagny is not an array and you meant to use another data type for this column after all, like text? - Dan Mironis
i'm trying to render the select name of compagny + icon in the next page when i submit my form. i did remove multiple=>true, and i don't have anymore the error , but the data is not save in the database and is also not render to the next page. why it doesn't work ? - Sirius
So, just for understand. Each post may have an array of compagny?, and compagny is an array, If compagny are the selected values, where are the full list of objects from where you expect to select a few? - abdiel

1 Answers

5
votes

Your $compagny property of Post is an array, just as you defined in the annotation:

/**
 * @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.

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