0

I feel like I am missing something obvious here, I have an entity form field that gives a list of users to select from , the idea here is creating a project and associate it with that (or several) users. I achieved this without much problems but I can't figure out how to access and work with that form field.

Here's how I am setting the form field:

->add('user', 'entity', array(
'class' => 'DevUserBundle:User',
'label'  => 'Assigned Users: ',
'multiple'=> true,))

In the controller I do the following:

$data = $form->getData();

I can access the field with $data['user'] but beyond that I am lost.

1 Answer 1

1

To see which user has been selected, the synthax is similar to what you suggested:

$usersSelected = $form["user"]->getData();

EDIT

The reason why you have such a long list in your print_r($userSelected) statement is because $userSelected is an array of User objects. Indeed, as you can see in your builder: ->add('user', 'entity'...)

You can verify this this way

$i = 1;
foreach ($usersSelected as $user)
{
    echo "User number ".$i;
    echo get_class($user);
    //Assuming that you have the method getUsername() in you User entity
    echo "Username is".$user->getUsername();
    $i++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

doing print_r on this gives a very long winded object which I was having problems with before, how do I navigate this object to get the usernames selected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.