put the data form database in the symfony 3 check box

advertisements

I started learn symfony 3. For the first project i chose a simple totodlist.

So now i have possibility to create and save the user in my database. Next I can create a task for them.

I want to create a checkbox where can i choose a users to perform a task.

So i need put data from my user database to checkbox form ($temp_users varbiable). I don't know how to do it.

Can anybody show me how to do it.

below is my code:

public function createAction(Request $request)
{

    $todo = new Todo;

    $users = $this->getDoctrine()
    ->getRepository('AppBundle:User')
    ->findAll();

    $temp_users = array();
    foreach($users as $user) {
    $temp_users[$user->getUsername()] = $user->getId();

}

 $form = $this->createFormBuilder($todo)
     ->add('name', TextType::class, array('attr' => array('class' => 'form-     control', 'style' => 'margin-bottom:15px')))

    ->add('wykona', CheckboxType::class, array('label' => $temp_users, 'required' => false,))


I'm not sure what you mean by checkbox - are you wanting to select one, or many users? I'm going to assume you want to select just one for simplicity's sake (you can adapt this to your liking)

You don't need to get all the users like you're trying to do, something like this should work (untested)

$form = $this->createFormBuilder($todo)
    ->add('user', EntityType::class, array(
        'label' => 'Name',
        'class' => AppBundle\Entity\User::class,
        'choice_label' => 'name', //if you have a variable 'name' in your User entity, otherwise you will need a __toString method in the User entity
    ));