1

我正在尝试制作一个包含一系列表格的表格,以便我填写每周数据。我有一个本周的实体,有一些统计数据

/**
 * @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 天的保留时间以及每周统计信息的其他字段?

任何解决方案的建议将不胜感激。

4

1 回答 1

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(),
           ));
     }
}
于 2013-10-14T20:11:22.873 回答