1
votes

Right now I have a problem with the query builder function of the entity form type inside Symfony.

Other questions on this board did not help me to find a solution:
See e.g.: 8456298 or 13846970 or enter link description here

Here's the situation: I want to add a email address to send a rating on request. Ratings are children of a Doctor class, which is a child of User (FOS User Bundle is used here)

So here's my code:

The controller:

public function AddPatientAction()
{
    $user = $this->getUser();
    $form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));
    return $this->render('Acme/DemoBundle:Dashboard/Premium:addpatient.html.twig', array(
                    'form' => $form
                ));
}

Here is the AddPremiumRatingType Class:

class AddPremiumRatingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('doctor', 'entity', array(
                'label' => 'Arztprofil',
                'required' => true,
                'class' => 'JBauleRatingBundle:Doctor',
                'mapped' => false,
                'property' => 'name',
                'query_builder' => function(DoctorRepository $er) use ($options) {
                    $user = $options['user'];
                    return $er->createQueryBuilder('d')
                                ->select('d')
                                ->where('d.user = ?1')
                                ->setParameter('1', $user->getId());
             }))
            ->add('emails', 'collection', array(
                'label' => 'E-Mail Adressen',
                'type'   => 'email',
                'mapped' => false,
                'delete_empty' => true,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'prototype_name' => '__name__',
                'options' => array(
                    'label' => 'E-Mail Adresse',
                    'attr' => array('class' => 'form-control'),
                    'required' => false
                )
            )) 
            ->add('save', 'submit', array(
                'label'     => 'Patienten hinzufügen'
            ));                  
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\PremiumRating',
        ));
        $resolver->setRequired(array(
            'user',
        ));
        $resolver->setAllowedTypes(array(
            'user' => 'Acme\LoginChildBundle\Entity\User',
        ));
    }

}

And finally here's the Error I get:

Expected argument of type "object, array or empty", "string" given

Also I tried:

  • Outsource the QueryBuild in a Repository Class and call the repository class => got the same Error

  • Only use the Create Query Command => Error that the query must be an instance of the QueryBuilder

  • Reading the Docs of Doctrine and Symfony ...couldn't find a solution yet

1

1 Answers

2
votes

Actually, you're unable to render your form at all, since you're receiving that error on creation.

$form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));

The second argument that createForm() receives is flagged as mixed. And as the error says, it have to be either an object or array() or simply null.

Change your line to:

$form = $this->createForm(new AddPremiumRatingType(), array(), array('user'=> $user));