Symfony2 无法将数据传递到表单下拉列表

Symfony2 trouble passing data into form dropdown

我正在学习 symfony,现在我在这个问题上被困了几个小时。我正在尝试从 FOSuserbundle 获取用户 ID 到我自己的表单的下拉列表中。但无法成功.. 我猜 __construct 功能有问题.. 这是代码

ShiftType.php:

class ShiftType extends AbstractType
{
    protected $users;

        public function __construct (User $users)
        {
            $this->users = $users;
        }

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

        $user = $this->user;

        $builder
            ->add('date', 'date', array(
                'label' => 'Shift Date',
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('site_name', 'text', array(
                'label' => 'Site Name',
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('location', 'text', array(
                'label' => 'Site Location',
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('startTime', 'time', array(
                'label' => 'Start time',
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('endTime', 'time', array(
                'label' => 'End time',
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

            ->add('employee', 'button', array(
                'class' => 'UserBundle:user',
                'label' => 'Select Employee',
                'attr' => array(
                    'data-toggle' => 'dropdown',
                    'class' => 'form-control btn btn-default dropdown-toggle',
                    'query_builder' => function(EntityRepository $er) use ($users) {
                            return $er->createQueryBuilder('pp')
                                ->where("pp.username = :username")
                                ->orderBy('pp.index', 'ASC')
                                ->setParameter('users', $users)
                            ;
                        },
                )
            ))


            ->add('save', 'submit', array(
                'attr' => array(
                    'class' => 'btn btn-lg btn-primary'
                )
            ));
    }

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

}

我也对我的 QueryBuilder 函数有疑问。如果我做的方式正确与否。

这是我的控制器:

public function shiftAction(Request $request)
    {
        $shift = new Shifts();

        $em = $this->getDoctrine()->getManager();

        $users = new User;

        $users = $em->getRepository('UserBundle:User')
                ->findAll();

        //var_dump($users);

        $form = $this->createForm(new ShiftType($users), $shift);

        $form->handleRequest($request);


        if ($form->isValid()) {

            $em->persist($shift);
            $em->flush();

            return $this->redirect($this->generateUrl('allshifts'));
        }

        return $this->render('XYZFirstBundle:Default:shifts.html.twig', array(
                'shiftForm' => $form->createView(),
            ));
    }   

我一直收到这个错误

ContextErrorException: Catchable Fatal Error: Argument 1 passed to XYZ\FirstBundle\Form\Type\ShiftType::__construct() must be an instance of XYZ\FirstBundle\Form\Type\User, array given, called in /var/www/html/learnsymfony/src/XYZ/FirstBundle/Controller/DefaultController.php on line 33 and defined in /var/www/html/learnsymfony/src/XYZ/FirstBundle/Form/Type/ShiftType.php line 12

public function __construct (User $users)

该行声明函数 __construct 期望将 User 对象传递给它,但在您的控制器中,您传递了一个数组

$users = $em->getRepository('UserBundle:User')->findAll(); // array of User objects

$form = $this->createForm(new ShiftType($users), $shift);

函数API:http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html#_findAll