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.

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.