我正在尝试使用一个表单集合的表单,这些表单允许我填写每周数据。我有一个实体,本周有一些统计数据
/**
* @ORM\Column(type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $week_id;
/**
* @ORM\Column(type="string")
*/
protected $area_worked;
/**
* @ORM\OneToMany(targetEntity="User")
*/
protected $approved_by;
/**
* @ORM\OneToMany(targetEntity="DailyStats")
*/
protected $daily_stats;
然后我有每日统计实体:
/**
* @ORM\Column(type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $day_id;
/**
* @ORM\ManyToOne(targetEntity="WeeklyStats")
*/
protected $weekly_stat_id;
/**
* @ORM\Column(type="float")
*/
protected $hours_worked;
/**
* @ORM\Column(type="integer")
*/
protected $day_of_week;
然后使用这两个我想要一个表格,我可以输出到一个表格显示整周:
Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
Hours | | | | | | |
然而,当我把它放到一个表格中时:
//weekly stats form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('dailyReports', 'collection',array(
'type'=>new DailyStatsForm(),
'options' => array(
'required' => false
),
'allow_add' => true,
));
}
这会生成一个带有空字段集的表单。我可以使用javascript向其中添加一个字段,但我想知道是否可以始终为此表单生成7天以及每周统计信息的其他字段?
非常感谢任何解决方案的建议。
答案 0 :(得分:4)
是的,您可以查看文档,如果您向周实体添加七个DailyStats实体,那么symfony2将呈现您想要的七个输入,请检查http://symfony.com/doc/current/cookbook/form/form_collections.html
class TaskController extends Controller
{
public function newAction(Request $request)
{
$task = new Task();
// dummy code - this is here just so that the Task has some tags
// otherwise, this isn't an interesting example
$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1); // any new related entity you add represents a new embeded form
$tag2 = new Tag();
$tag2->name = 'tag2';
$task->getTags()->add($tag2);
// end dummy code
$form = $this->createForm(new TaskType(), $task);
$form->handleRequest($request);
if ($form->isValid()) {
// ... maybe do some form processing, like saving the Task and Tag objects
}
return $this->render('AcmeTaskBundle:Task:new.html.twig', array(
'form' => $form->createView(),
));
}
}