0

I have a problem with my query builder but I do not know how to fix this error can you help me please?

So here is my problem, I want to retrieve the list of questions that are not in my questionnaire and have the same theme as my questionnaire.

Here is my code:

    $builder
        ->add('orderQuestion')
        ->add('idQuestion', EntityType::class, [
            'class' => Question::class,
            'query_builder' => function(EntityRepository $er) use ($idTheme, $idQuestionnaire){
                $resultatQuestion = $er->createQueryBuilder('questionn')
                    ->select('questionn.id')
                    ->innerJoin('App\Entity\SurveyQuestion', 'surveyQuestion', 'WITH', 'questionn.id = surveyQuestion.idQuestion')
                    ->where('surveyQuestion.idSurvey = :idSurvey')
                ;

                $resultat = $er->createQueryBuilder('q')
                    ->leftJoin('q.surveyQuestions', 'sQ')
                    ->leftJoin('sQ.idSurvey', 's')
                    ->where('q.idTheme = :idTheme')->setParameter('idTheme', $idTheme)->setParameter(':idSurvey', $idQuestionnaire)
                    ->andWhere($er->createQueryBuilder('question')->expr()->notIn('q.id', $resultatQuestion->getDQL()))
                    ;

                return $resultat;
            },
            'choice_label' => function ($question) {
                return $question->getLabel();
            },
        ])
    ;

But, with this code a have this error : "Warning: get_class() expects parameter 1 to be object, array given".

How can I solve this problem ?

JohanL11
  • 21
  • 7
  • 2
    Did you tried to remove `->getQuery()->getResult()` in the last query? You need to return a `QueryBuilder` object. Not the result of the query. – Evgeny Ruban May 08 '19 at 12:56
  • Yes i try this, but i error come because my first request return an array but i need this information for my second request – JohanL11 May 08 '19 at 13:26
  • I'm talking about yours second request, not about first. The error is not about your first query, it's about value you returning. And it seems that you need to get just ids in the first query. – Evgeny Ruban May 08 '19 at 13:31
  • Yes, that's exactly what I need – JohanL11 May 08 '19 at 13:54
  • So, is this solved your problem? I have updated my answer. – Evgeny Ruban May 08 '19 at 16:18
  • No, your solution does not work – JohanL11 May 08 '19 at 16:56
  • Can you show your updated code? – Evgeny Ruban May 08 '19 at 17:04
  • I put my new code – JohanL11 May 08 '19 at 17:43
  • Yeap, I see. But you just need to update your question and add it there, not as an answer. So, look, also try to remove `->getResult()` from the first query, just according to [IN operator](https://www.w3schools.com/sql/sql_in.asp). – Evgeny Ruban May 08 '19 at 17:57
  • I try to remove ->getResult() from my first query but I still have an error that is "Expression of type 'Doctrine\ORM\Query' not allowed in this context." – JohanL11 May 08 '19 at 18:48
  • 1
    [This question](https://stackoverflow.com/questions/13957330) might help. – msg May 08 '19 at 19:31
  • Yeap, using `$test->getDQL()` in the second query might help – Evgeny Ruban May 08 '19 at 21:28
  • I used the method of msg, it works because when I use this request on PhpMyAdmin it works but in my select it shows me the list of all my questions – JohanL11 May 09 '19 at 08:08
  • I update my code and its works – JohanL11 May 09 '19 at 08:47
  • It is not good that you have changed your code in the question, you could just add correct code as update in it. And it's a bit overengineering here `->andWhere($er->createQueryBuilder('question')->expr()->notIn('q.id', $resultatQuestion->getDQL()));`. It should work just with `->andWhere('q.id NOT IN ' . $resultatQuestion->getDQL())` as I mentioned in my answer. – Evgeny Ruban May 09 '19 at 09:23

2 Answers2

-1

So, to be clear, as I mentioned in the comment and in the Symfony's documentation - 'query_builder' can either be a QueryBuilder object, a callable or null.

This way you need to remove ->getQuery() and ->getResult() from your second query.

Update

So, thanks for the clarification from @msg in comments. And according it, you also need to remove from the first statement ->getQuery() and ->getResult() too and just add there ->getDQL()

And it should work.

Evgeny Ruban
  • 1,357
  • 1
  • 14
  • 20
-1

could you modify your code

$resultat = $er->createQueryBuilder('q')
                    ->leftJoin('q.surveyQuestions', 'sQ')
                    ->leftJoin('sQ.idSurvey', 's')
                    ->where('q.idTheme = :val')
                    ->andWhere('q.id NOT IN ' . $test)
                    ->setParameter('val', $idTheme)
                    ->getQuery()
                    ->getResult()
                    ;