Complex SelectQuery with multiple left joins and conditions with OR I have a view and in hook_views_pre_execute() I have to add multiple left joins with conditions. Currently I'm doing this:

$or = db_or();
foreach ($fields as $field) {
    $query-&gt;leftJoin($field-&gt;table, $field-&gt;table, '(node.nid = ' . $field-&gt;table . '.entity_id)');
    $or = $or-&gt;condition($field-&gt;table . '.' . $field-&gt;field, '%' . $string . '%', 'LIKE');
}

$query-&gt;condition($or);


but it doesn't seem to work.
It seems that the problem was $field-&gt;field. For example if I have a field called field_book_author in database the $field-&gt;table would be field_data_field_book_author and the value of the field would be in field_book_author_value.

So the correct code is:

$or = db_or();
foreach ($fields as $field) {
    $query-&gt;leftJoin($field-&gt;table, $field-&gt;table, '(node.nid = ' . $field-&gt;table . '.entity_id)');

    if (property_exists($field, 'additional_fields') &amp;&amp; (count($field-&gt;additional_fields) &gt; 3)) {
        $field_name = $field-&gt;additional_fields[3];
    }else {
        $field_name = $field-&gt;field;
    }
    $or-&gt;condition($field-&gt;table . '.' . $field_name, '%' . $string . '%', 'LIKE');
}

$query-&gt;condition($or);