在Symfony2 FORM中选择多个

时间:2012-09-27 11:05:17

标签: symfony

我有这个场景

EntityAEntityB相关的一对多方式。我为EntityB创建了一个表单,我想选择(带有复选框)部分EntityA元素。

我怎样才能做到这一点?

更新

public function viewAction()
{
    $id_struttura = 9;
    $channel_rates = $this->getDoctrine()->getRepository('SestanteChannelManagerBundle:StrutturaCanaleTariffa')->findByStruttura($id_struttura);
    $list = array();
    foreach ($channel_rates as $channel_rate) {
        $list[$channel_rate->getId()] = $channel_rate->getNomeCameraTariffa();
    }
    $view = new View();
    $view->addChannelRate($channel_rates);
    $form = $this->createForm(new ViewType(), $view);
    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            $dateRange = new DateRange($date_from, $date_to);
            $channelDisp = $this->get('channel_dispatcher');
            $inventories = $channelDisp->queryAvailabilityRates($dateRange, $channelRateList);
            return $this->render('SestanteCDTestBundle:Main:view_result.html.twig', array('inventories' => $inventories));
        }
    }
    return $this->render('SestanteCDTestBundle:Main:view.html.twig', array('form' => $form->createView()));
}

这是我的实体

<?php

namespace Sestante\CDTestBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

class View
{
    protected $channel_rate;

    function __construct()
    {
        $this->channel_rate = new ArrayCollection();
    }


    /**
     * @return the $date_from
     */
    public function getDateFrom() {
        return $this->date_from;
    }

    /**
     * @return the $date_to
     */
    public function getDateTo() {
        return $this->date_to;
    }

    /**
     * @return the $channel_rate
     */
    public function getChannelRate() {
        return $this->channel_rate;
    }

    /**
     * @param field_type $date_from
     */
    public function setDateFrom($date_from) {
        $this->date_from = $date_from;
    }

    /**
     * @param field_type $date_to
     */
    public function setDateTo($date_to) {
        $this->date_to = $date_to;
    }

    /**
     * @param field_type $channel_rate
     */
    public function addChannelRate($channel_rate) {
          $this->channel_rate[] = $channel_rate;
    }
}

这是我的表格

<?php

namespace Sestante\CDTestBundle\Form;

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

class ViewType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('date_from', 'date', array('widget' => 'single_text','format' => 'yyyy-MM-dd'));
        $builder->add('date_to', 'date', array('widget' => 'single_text','format' => 'yyyy-MM-dd'));
        $builder->add('channel_rate', 'entity', array('class'=>'SestanteChannelManagerBundle:StrutturaCanaleTariffa', 'multiple'=>true, 'expanded'=>true));
    }

    public function getName()
    {
        return 'view';
    }
}

如果我添加这样的声明(进入实体),所有效果都很好:

foreach ($channel_rate as $ch)
{
$this->channel_rate[] = $ch;
}

1 个答案:

答案 0 :(得分:5)

在表单的buildForm方法中:

$builder->add('entitya', 'entity', array(
    'class'     => 'YourAwesomeBundle:EntityA',
    'expanded'  => true,
    'multiple'  => true
));

reference中所述。

编辑查看您的代码:

/**
 * @param field_type $channel_rate
 */
public function addChannelRate($channel_rate) {
      $this->channel_rate[] = $ch;
}

$ch未在此方法中定义,您需要编写`$ this-&gt; channel_rate [] = $ channel_rate。

无论如何,提供setChannelRate方法,如:

/**
 * @param ArrayCollection $rates
 */
public function setChannelRate($rates) {
      $this->channel_rate = $rates;
}

然后重试。