我有这种情况
EntityA
相关EntityB
成一对多的方式。我已经创建了一个表单,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;
}