gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:52:43 25 4
gpt4 key购买 nike

我有一个下拉列表,其中包含实体列表 + 实体旁边的图标。但是当我提交我的表单时,我得到了这个错误:

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 %}

新的.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>

最佳答案

你的 Post 的 $compagny 属性是一个数组,就像你在注解中定义的那样:

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

阅读 doctrine 文档,这个数组在存储到数据库之前将被序列化。

而且你不能直接在 twig 中渲染它。

您需要使用for 将数组中的项目逐一显示。

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

关于php - 数组到字符串的转换。 symfony2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com