I need to add a OR condition to my query so that I can get all nodes related to a particular term. Here is what I have done :
$query = \Drupal::database()->select('taxonomy_index', 'ti');
$query->fields('ti', ['nid']);
// OR CONDITION
$or_group = $query->orConditionGroup()->condition('ti.tid', $params[1], 'IN');
$query->condition('ti.tid', $params[0], 'IN');
$query->condition('ti.status', 1);
// Added OR CONDITION TO QUERY.
$query->condition($or_group);
$query->execute()->fetchAllAssoc('nid');
This is the how the query get build :
QUERY
SELECT ti.nid AS nid
FROM {taxonomy_index} ti
WHERE (ti.tid IN (:db_condition_placeholder_0))
AND (ti.status = :db_condition_placeholder_1)
AND (ti.tid IN (:db_condition_placeholder_2));
PARAMS
Array (
[:db_condition_placeholder_0] => 170
[:db_condition_placeholder_1] => 1
[:db_condition_placeholder_2] => 227
);
As you can see the last "AND" condition should be "OR".
Thanks.
orConditionGroup
adds a 'OR' conditon to the query.