How can I combine two querybuilder in Symfony formbuilder?

In my formbuilder I am using querybuilder to get the firstname:

$options['class'] = 'AppEntityData'; 
$options['choice_label'] = 'content';

 $firstname = "81";
  $options['query_builder'] = function (EntityRepository $er) use ($firstname) {
       return $er->createQueryBuilder('data')
         ->andWhere('data.fields = :field')
         ->setParameter('field', $firstname)
       ;
  }

The output is Fred

Alternatively I can also get the company:

$lastname = "35";
$options['query_builder'] = function (EntityRepository $er) use ($lastname) {
        return $er->createQueryBuilder('data')
            ->andWhere('data.fields = :field')
            ->setParameter('field', $lastname)
         ;
 };

The output is Miller

How can I combine those two queries into one?

The output I am looking for is Fred Miller

My approach:

$firstname = "81";
$lastname = "35";
$options['query_builder'] = function (EntityRepository $er) use ($firstname, $lastname) {
            return $er->createQueryBuilder('data')
            ->andWhere('data.fields = :firstname')
            ->andWhere('data.fields = :lastname')
             ->setParameter('firstname', $firstname)
             ->setParameter('lastname', $lastname)
         ;

But I get just an empty output.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *