Skip to content

Instantly share code, notes, and snippets.

@evgeniy1204
Last active April 21, 2018 10:34
Show Gist options
  • Save evgeniy1204/03dec32839d977a8ffe2d0a8bccd22ae to your computer and use it in GitHub Desktop.
Save evgeniy1204/03dec32839d977a8ffe2d0a8bccd22ae to your computer and use it in GitHub Desktop.
rep
<?php
/**
* 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