Symfony2:将Entitytype转换为Checkboxes / Radiobutton

时间:2016-10-06 10:23:35

标签: php forms symfony doctrine-orm doctrine

对于我的Symfony项目,我使用的是从另一个图表中获取其字段数据的表单。数据本身显示正确,但预订字段(预订)和房间设备(Zimmerausstattung)字段存在一些问题。

Form with EntityType

我的表格代码:


class ZimmerType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('zimmerNummer', TextType::class, array('label' => 'Zimmer: ', 'required' => false, 'attr' => array('placeholder' => 'Nr. oder Name')))
            ->add('betten', IntegerType::class, array('label' => 'Betten: *', 'attr' => array('placeholder' => 'Anzahl der Betten')))
            ->add('standort', TextType::class, array('label' => 'Standort: ', 'required' => false, 'attr' => array('placeholder' => 'Etage oder Ort des Zimmers')))
            ->add('zimmerausstattung', EntityType::class, array('label' => 'Zimmerausstattung: ',
                                                                'class' => 'AppBundle:Zimmerausstattung',
                                                                'multiple'    => true,
                                                                'required'    => false,
                                                                'empty_data'  => null,
                                                                ))
            ->add('location', EntityType::class, array('label' => 'Location: *', 
                                                       'class' => 'AppBundle:Location'))
            ->add('booking', EntityType::class, array('label' => 'Booking: ',
                                                                'class' => 'AppBundle:Booking',
                                                                'multiple' => true,
                                                                'required'    => false,
                                                                'empty_data'  => null,
                                                                ))
; }

我将整个表单转换为“Checkbutton / Radiobutton” - 虽然转换工作正常但没有显示任何记录。

Form with Checkboxes


class ZimmerType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('zimmerNummer', TextType::class, array('label' => 'Zimmer: ', 'required' => false, 'attr' => array('placeholder' => 'Nr. oder Name')))
            ->add('betten', IntegerType::class, array('label' => 'Betten: *', 'attr' => array('placeholder' => 'Anzahl der Betten')))
            ->add('standort', TextType::class, array('label' => 'Standort: ', 'required' => false, 'attr' => array('placeholder' => 'Etage oder Ort des Zimmers')))
            ->add('zimmerausstattung', EntityType::class, array('label' => 'Zimmerausstattung: ',
                                                                'class' => 'AppBundle:Zimmerausstattung',
                                                                'multiple'    => true,
                                                                'required'    => false,
                                                                'empty_data'  => null,))
                                                                'expanded'    => true,))
            ->add('location', EntityType::class, array('label' => 'Location: *', 
                                                       'class' => 'AppBundle:Location'))
            ->add('booking', EntityType::class, array('label' => 'Booking: ',
                                                                'class' => 'AppBundle:Booking',
                                                                'multiple' => true,
                                                                'required'    => false,
                                                                'empty_data'  => null,
                                                                'choice_label' => 'person.name',
                                                                'expanded'    => true,
                                                                ))
; }

My Twig-Code:

{% extends '::base.html.twig' %}
{% form_theme edit_form 'form_table_layout.html.twig' %}
{% block title %}Zimmer {{ entity.zimmernummer }} bearbeiten {% endblock %}
{% block body -%}
<h1>{{ block('title') }}</h1>

{{ form(edit_form) }}

    <ul class="record_actions">
<li>
    <a href="{{ path('zimmer') }}">
        Zurück zur Übersicht
    </a>
</li>
<li>{{ form(delete_form) }}</li>
</ul>
{% endblock %}

我该如何解决这个问题? 如果有人可以帮我解决这个问题,那将是非常好的,因为我的想法已经不多了。

2 个答案:

答案 0 :(得分:0)

只需添加引用objects参数的choice_label即可。示例:'choice_label' => 'title'其中title是您的实体字段。

文档: http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label

答案 1 :(得分:0)

我的最终解决方案:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
    var cell:commentTableViewCell
    if commentList[indexPath.row].cellType == 1{
        //drink
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "drink",for: indexPath) as! commentTableViewCell
        cell.drinkName.text = commentList[indexPath.row].commentOwner
        cell.drinkPrice.text = commentList[indexPath.row].comment

        return cell
    }
    else{
        //comment
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "comment",for: indexPath) as! commentTableViewCell
        cell.ownerTitle.text = commentList[indexPath.row].commentOwner
        cell.commentContent.text = commentList[indexPath.row].comment
        if commentList[indexPath.row].isFile{
            //cell.imageView!.contentMode = UIViewContentMode.scaleAspectFill
            cell.imageView!.image = commentList[indexPath.row].imageFile
        }
        else if !commentList[indexPath.row].isFile {
            cell.imageView!.image = nil
            cell.imageView!.removeFromSuperview()

        }
        return cell
    }
}