如何在symfony2中为集合类型设置字段值

时间:2012-03-14 11:21:30

标签: symfony formbuilder

// - 表格timesheettype ---它不是实体类

class TimeSheetType extends AbstractType {

        public function buildForm(FormBuilder $builder, array $options) {
            $builder->
                    add('dailyTimeSheet', 'collection', array('type' => new DailyType(), 'allow_add' => true, 'allow_delete' => true, 'prototype' => true,))
                    ->add('comment','textarea');
        }

         public function getName() {
            return 'TimeSheetDaily';
        }

    }

// --- DailyType - 有这种类型的实体

class DailyType extends AbstractType {


    public function buildForm(FormBuilder $builder, array $options) {


        $builder->add('project','entity',array('class'=> 'Parabola\EntityBundle\Entity\Project','property'=>'name'))
               ->add('projectTask', 'entity', array('class'=> 'Parabola\EntityBundle\Entity\ProjectTask','property'=>'name'))
                ->add('hours', 'text');

    }
    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Parabola\EntityBundle\Entity\TimeSheetDaily');
    }

 public function getName() {
    return 'DailySheet';
}

// - 控制器 -

$repository = $this->getDoctrine()
                ->getRepository('ParabolaEntityBundle:TimeSheetDaily')->findAll();
        $form = $this->createForm(new \Parabola\TimeSheetBundle\Form\TimeSheetType(),$repository);

我有实体类TimeSheetDaily。在构建TimeSheetType的形式时,我将TimeSheetDaily类对象的数组传递给表单类型。并且TimeSheetType具有DailyType的集合。它不是集合字段的设置值,它只是一个TimeSheetDaily实体。

1 个答案:

答案 0 :(得分:0)

您是否定义了__constructor课程的TimeSheetDaily? 你有类似的东西吗?

public function __construct(){
//....

$this->dailyTimeSheet = new ArrayCollection();

//....
};

正确初始化此集合非常重要,以便Symfony能够将数据插入其中....