Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/0210a9080c73ecc704ff to your computer and use it in GitHub Desktop.
Save lvidarte/0210a9080c73ecc704ff to your computer and use it in GitHub Desktop.
Models\Products.php
private function queryBuilder($params)
{
$queryBuilder = $this->conn->createQueryBuilder();
$queryBuilder
->select(
'p.id', 'p.nombre name', 'precio_ref referencePrice',
'p.categoria_id categoryId', 'precio_venta sellPrice',
'img_catalogo image', 'descripcion description',
'publicado published', 'alt imageAlt',
'short_description shortDescription')
->from('productos', 'p')
->innerJoin('p', 'categorias', 'c', 'c.id=p.categoria_id')
->orderBy('p.orden');
$translations = array(
'categoryId' => 'p.categoria_id',
'campaignId' => 'c.venta_id',
'published' => 'p.publicado'
);
foreach ($params as $key => $value) {
if (isset($translations[$key])) {
if (is_array($value)) {
$queryBuilder
->andWhere(sprintf('%s IN (:%s)', $translations[$key], $key))
->setParameter($key, $value, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);
} else {
$queryBuilder
->andWhere(sprintf('%s=:%s', $translations[$key], $key))
->setParameter($key, $value);
}
}
}
return $queryBuilder;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment