Krzysztof Michalski
Krzysztof Michalski

Reputation: 831

Doctrin's Query Builder. Query with AND in where clause

I have a query in Query Builder in Doctrine. My query is:

$result = $this->entityManager->createQueryBuilder()
                    ->select('cc', 'cct', 'cces')->from('App\Http\Entities\Cic\CaseCategory', 'cc')
                    ->innerJoin('cc.type', 'cct')
                    ->leftJoin('cc.eventSubject', 'cces')
                    ->orderBy('cc.title')
                    ->where('cc.active = 1')
                    ->getQuery();

How Could I get query with AND clause? I mean to replace cc.active = 1 AND system_category=1' instead cc.active = 1 in where clause. I'm trying in that way:

$result = $this->entityManager->createQueryBuilder()
                    ->select('cc', 'cct', 'cces')->from('App\Http\Entities\Cic\CaseCategory', 'cc')
                    ->innerJoin('cc.type', 'cct')
                    ->leftJoin('cc.eventSubject', 'cces')
                    ->orderBy('cc.title')
                    ->where('cc.active = 1 AND system_category=1')
                    ->getQuery();

But in that way it's dosen't work. How could I do that correctly? I would be greateful for help. Best regards

Upvotes: 0

Views: 98

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

try this:

$result = $this->entityManager->createQueryBuilder()
   ->select('cc', 'cct', 'cces')->from('App\Http\Entities\Cic\CaseCategory', 'cc')
   ->innerJoin('cc.type', 'cct')
   ->leftJoin('cc.eventSubject', 'cces')
   ->orderBy('cc.title')
   ->where('cc.active = 1')
   ->andWhere('system_category=1')
   ->getQuery();

I suggest to you to use parameters like this:

$result = $this->entityManager->createQueryBuilder()
       ->select('cc', 'cct', 'cces')->from('App\Http\Entities\Cic\CaseCategory', 'cc')
       ->innerJoin('cc.type', 'cct')
       ->leftJoin('cc.eventSubject', 'cces')
       ->orderBy('cc.title')
       ->where('cc.active = :active')
       ->andWhere('system_category=:system_category')
       ->setParameters(
          [
              'active' => 1,
              'system_category' => 1
          ]
       )
       ->getQuery();

Upvotes: 1

Related Questions