简体   繁体   中英

Symfony2 trouble passing data into form dropdown

I am learning symfony, I am stuck on this problem for hours now. I am trying to get userIDs from FOSuserbundle into my own form's Dropdown. but unable to succeed.. am doing something wrong with the __construct function I guess.. here's the code

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';
    }

}

I have doubts about my QueryBuilder function as well. If I am doing it the right way or not.

This is my Controller :

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(),
            ));
    }   

All the time I am getting this error

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)

That line stated that the function __construct expect a User object to be passed into it, but in your controller, you pass an array

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM