私はこのシナリオを持っています
EntityA
1対多の方法で関連付けられ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;
}