Created
April 21, 2018 10:34
-
-
Save evgeniy1204/86fb8e93eee141cd2f94bf418d4a362e to your computer and use it in GitHub Desktop.
rep
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
/** | |
* Search products with options | |
* | |
* @param array $options | |
* | |
* @return array | |
*/ | |
public function searchProducts(array $options = []) | |
{ | |
$qb = $this->createQueryBuilder('s') | |
->select('s'); | |
$qb->leftJoin('ProductBundle:ProductVariation', 'productVariation', 'WITH', 'productVariation.product = s.id'); | |
$qb->andWhere('productVariation.isAvailable = 1') | |
->andWhere('productVariation.amount > 0'); | |
if ($options['user'] instanceof User) { | |
$qb | |
->andWhere(sprintf('s.bodyType = \'%s\'', $options['user']->getBodyTypeUser())); | |
} | |
if ($options['order_by']) { | |
list($sortField, $sortBy) = explode(":", $options['order_by']); | |
$qb->orderBy(sprintf('s.%s', $sortField), strtoupper($sortBy)); | |
} | |
if ($options['page']) { | |
$options['limit'] *= (int)$options['page']; | |
} | |
$qb->setMaxResults($options['limit']); | |
dump($qb->getQuery()); | |
dump($qb->getQuery()->getResult()); | |
die; | |
if ($options['return_qb']) { | |
return $qb; | |
} | |
return $qb->getQuery()->getResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment