Reputation: 120
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
Upvotes: 0
Views: 230
Reputation: 2289
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
Upvotes: 1