我有这种情况
EntityA涉及EntityB到一个一对多的方式.我已经创建了一个表单EntityB,我想选择(带有复选框)一些EntityA元素.
我怎样才能做到这一点?
UPDATE
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()));
}
Run Code Online (Sandbox Code Playgroud)
这是我的实体
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的形式
<?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';
}
}
Run Code Online (Sandbox Code Playgroud)
如果我添加这样的声明(进入实体),所有作品都很好:
foreach ($channel_rate as $ch)
{
$this->channel_rate[] = $ch;
}
Run Code Online (Sandbox Code Playgroud)
在您的表单buildForm方法中:
$builder->add('entitya', 'entity', array(
'class' => 'YourAwesomeBundle:EntityA',
'expanded' => true,
'multiple' => true
));
Run Code Online (Sandbox Code Playgroud)
如参考文献中所述.
编辑看你的代码:
/**
* @param field_type $channel_rate
*/
public function addChannelRate($channel_rate) {
$this->channel_rate[] = $ch;
}
Run Code Online (Sandbox Code Playgroud)
$ch 在这个方法中没有定义,你需要写`$ this-> channel_rate [] = $ channel_rate.
无论如何,提供一种setChannelRate方法,如:
/**
* @param ArrayCollection $rates
*/
public function setChannelRate($rates) {
$this->channel_rate = $rates;
}
Run Code Online (Sandbox Code Playgroud)
然后重试.
| 归档时间: |
|
| 查看次数: |
9036 次 |
| 最近记录: |