I have following function:
public function latestNews($tags = array(), $categories = array(), $authors = array(), $lang = 'en', $source = '', $limit = 20) {
return $this->createQueryBuilder('News')
->field('tags')->in($tags)
->field('categories')->in($category)
->field('authors')->in($authors)
->field('lang')->equals($lang)
->sort('date' -> 'DESC')
->field('source')->equals($source)
->limit($limit)
->getQuery()
->execute();
}
I want if variables such as $tags
, $categories
, $authors
or $source
provided by function caller this variables affect on the createQueryBuilder
, but if each of them does not provide by the function caller(variable with default value) they don't affect createQueryBuilder and make this condition neutral on query.
One way is I make the query with many if condition but it is very messy.
Is there any better solution?