Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Forked from janmarek/gist:890638
Created March 28, 2011 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fprochazka/890879 to your computer and use it in GitHub Desktop.
Save fprochazka/890879 to your computer and use it in GitHub Desktop.
<?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