-
-
Save fprochazka/890879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Query\Expr\Join; | |
class OfferFinder extends Blabla | |
{ | |
public function whereTags(array $tags) | |
{ | |
if (count($tags) === 0) { | |
return; | |
} | |
// rozhodně by to šlo udělat alespoň jednou query, | |
// ale myslel jsem že zadání je "dej mi věci označené alespoň jedním z těchto tagů a alespoň jedním z tamtěch tagů" :) | |
$ids = array_map(function ($tag) { | |
return $tag->getId(); | |
}, $tags); | |
// hodilo by se vidět i ty dvě/tři entity | |
$this->qb->innerJoin('OfferTag', 'ot', Join::WITH, $this->qb->expr()->in('ot.id', $ids)) | |
return $this; | |
} | |
public function whereTagsOrTags(array $tagsGroup1, array $tagsGroup2) | |
{ | |
if (count($tagsGroup1) === 0 || count($tagsGroup2) === 0) { | |
return; | |
} | |
$mapper = function ($tag) { | |
return $tag->getId(); | |
}; | |
$idsOne = array_map($mapper, $tagsGroup1); | |
$idsTwo = array_map($mapper, $tagsGroup2); | |
// kombinace LEFT JOIN a WHERE NOT NULL | |
$this->qb->leftJoin('OfferTag', 'ot1', Join::WITH, $this->qb->expr()->in('ot1.tag_id', $idsOne)); | |
$this->qb->leftJoin('OfferTag', 'ot2', Join::WITH, $this->qb->expr()->in('ot2.tag_id', $idsTwo)); | |
$this->qb->andWhere('ot1.id IS NOT NULL')->orWhere('ot2.id IS NOT NULL'); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment