dqtl46964 2013-09-27 07:09
浏览 36

Symfony2形成+学说 - 选择的选择

Ok guys,

I've got table build based on entity with integer fields,

id, user_id, project_id, stats, risks.

Now I'm trying to build form with only fields stats, risk. So I select by userId and projectId:

$dql = "SELECT m FROM DevDashProjectBundle:Module m WHERE m.user ="
    .$options['user']->getId()."AND m.project ="
    .$options['project']->getId();
$results = $this->entityManager->createQuery($dql)->getArrayResult();  

Now I want to build form where fields stats with value >0 (or true or sth) is selected and 0 (or false) is not selected. Next after editing form I want to post changes to this field - so selected field is 1, not selected 0.

@Edit

My entity looks like this:

 /**
 * @ORM\Column(name="stats", type="boolean", nullable=false)
 * 
 * @var boolean
 */
protected $stats=true;

Default value is true as you see.

In controller I'm passing values like this:

  $form = $this->createForm(new ModuleType($em), $module, array('user' => $user, 'project' => $project));

$user and $project are required to select proper row.

Next in ModuleType

        $builder
        ->add('module', 'checkbox', array(
            'label' => 'Stats',
        ));

The table module, got field stats which is tinyint (interpreted as boolean by Symfony) set as 1 but the checkbox stats is still unchecked.

I was also trying sth like this:

        $builder
        ->add('module', 'entity', array(
            'class' => 'DevDashProjectBundle:Module',
            'mapped' => true,
            'multiple' => true,
            'expanded' => true,
            'property' => 'stats',
            'query_builder' => function(EntityRepository $er) use ($options){
                return $er
                    ->createQueryBuilder('m')
                    ->where(
                        'm.user =' .$options['user']->getId().
                        'AND m.project=' .$options['project']->getId()
                    );
            }
        )
    );

Beacuse I need to select stats by user_id and project_id. The query returns correct values, but checkboxes with value true are still unchecked.

  • 写回答

1条回答 默认 最新

  • doukuanjing5404 2013-09-27 09:22
    关注

    You would build your form on the Module entity like this:

    $form = $this->createFormBuilder($module)
            ->add('stats', 'checkbox', array('label' => 'Stats'))
            ->getForm();
    

    But the stats field in the entity has to be declared as boolean. See this question also: PHP/Symfony2 Form Checkbox field

    Edit: This: $builder->add('module', 'checkbox', array('label' => 'Stats'));

    Should be $builder->add('stats', 'checkbox', array('label' => 'Stats'));

    To match the name of your field.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题