Home > Software design >  Expression of type 'App\Entity\User' not allowed in this context in FormType in Symfony
Expression of type 'App\Entity\User' not allowed in this context in FormType in Symfony

Time:11-10

I am trying to filter in a form the recipients that belong to the logged in user.

I have the above error when I try to filter my Destinataire table with the logged in user.

Expression of type 'App\Entity\User' not allowed in this context

Utilisateur is a field of my Destinataire entity. My form :

<?php

namespace App\Form;

use App\Entity\Courrier;
use App\Entity\Destinataire;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;

class CourrierType extends AbstractType
{
  private $security;

  public function __construct(Security $security)
  {
    $this->security = $security;
  }

  public function buildForm(FormBuilderInterface $builder, array $options): void
  {
    $builder
        ->add('objet')
        ->add('destinataire', EntityType::class, [
            'class' => Destinataire::class,
            'placeholder' => '--Choisissez un destinataire--',
            'query_builder' => function (EntityRepository $er) 
            {
                $user = $this->security->getUser();
                return $er->createQueryBuilder('qb')
                    ->where('utilisateur', $user)
                    ->addOrderBy('qb.denomination', 'ASC')
                    ->addOrderBy('qb.prenom', 'ASC')
                    ->addOrderBy('qb.nom', 'ASC');
            },
        ])           
        ->add('dateEnvoi')
        ->add('dateRelance')
        ->add('statut')
        ->add('offreReference')
        ->add('nosReferences')
        ->add('vosReferences')
        ->add('annonceCopie')
        ->add('paragraphe1')
        ->add('paragraphe2')
        ->add('paragraphe3')
        ->add('paragraphe4')  
    ;
  }

  public function configureOptions(OptionsResolver $resolver): void
  {
    $resolver->setDefaults([
        'data_class' => Courrier::class,
    ]);
  }
}

The error is on the line :

->where('utilisateur', $user)

CodePudding user response:

Replace this line ->where('qb.utilisateur = :user')->setParameter('user', $user)

For more info read this documentation Doctrine Binding Parameters

  • Related