EntityFieldQuery with pager and db_or using addTag I'm having an issue that I hope some of you can help me with.

Drupal version: 7.36

I've created a list of all nodes of a certain content type using EntityFieldQuery. Since there are a lot of nodes with this content there's also pagination with 6 nodes pr page (not that it matters). Right above my list I have a little search field, where the user can search within this list of nodes with the specified content type. With this I'm using the addTag and db_or method described here: Using hook_query_TAG_alter to add &#39;OR&#39; queries to EntityFieldQuery. With this the user searches both in the title and the content.

However. For some reason it doens't quite work while having $query-&gt;pager() or $query-&gt;range() and using the $query-&gt;leftJoin() function at the same time. If I outcomment the $query-&gt;leftJoin() and the connected db_or-&gt;condition() everything works fine. Or if I outcomment the $query-&gt;pager(6) everything works fine as well.

Here is all my code for getting the list of content types:

function node_load_by_type($type, $options = array()) {
    $settings = array(
        'limit' =&gt; array_key_exists('limit', $options) ? $options['limit'] : -1,
        'pager' =&gt; array_key_exists('pager', $options) ? true : false,
        'offset' =&gt; array_key_exists('offset', $options) ? $options['offset'] : 0,
        'orderDirection' =&gt; array_key_exists('orderDirection', $options) ? $options['orderDirection'] : 'DESC',
        'keywords' =&gt; array_key_exists('keywords', $options) ? $options['keywords'] : false,
    );

    $query = new EntityFieldQuery();
    $query-&gt;entityCondition('entity_type', 'node')
        -&gt;entityCondition('bundle', $type)
        -&gt;propertyCondition('status' , 1)
        -&gt;propertyCondition('nid', arg(1), '!=')
        -&gt;propertyOrderBy('created', $settings['orderDirection']);

    if ($settings['limit'] !== -1 &amp;&amp; $settings['pager']) {
        $query-&gt;pager($settings['limit']);
    } else if ($settings['limit'] !== -1) {
        $query-&gt;range($settings['offset'], $settings['limit']);
    }

    if ($settings['keywords']) {
        $query-&gt;addTag('search');
        $query-&gt;addMetaData('keywords', $settings['keywords']);
    }

    $results = $query-&gt;execute();

    if (is_array($results) &amp;&amp; array_key_exists('node', $results)) {
        $results = node_load_multiple(array_keys($results['node']));

        if ($settings['limit'] !== -1 &amp;&amp; $settings['pager']) {
            $results = theme('article_list', array('nodes' =&gt; $results, 'pager' =&gt; theme('pager', $query-&gt;pager)));
        }
    }

    return $results;
}

function THEME_query_search_alter(QueryAlterableInterface $query) {
    $keywords = $query-&gt;getMetaData('keywords');
    $words = "%" . $keywords . "%";

    $or = db_or()-&gt;condition('title', $words, 'LIKE');
    $query-&gt;leftJoin('field_data_body', 'n', 'node.nid = n.entity_id');
    $or-&gt;condition('n.body_value', $words, 'LIKE');
    $query-&gt;condition($or);
}


Has anyone experienced this before and found the solution? Or can simply see in my code somewhere that there's an error which is causing my problem.
Ideas for doing it differently is also welcome.

Thanks in advance. :)



UPDATE:

More testing tells me that what triggers this is actually joining another database table into the $query. If I don't use pagination but still the $query-&gt;leftJoin(), $query-&gt;join() or any similar functions the search actually returns a count of a lot of results even though there might only be 1 result.

Using the global variable $pager_total_items I can see that when there's actually only 1 result, it tells me that there are 1917 results. Then the pagination goes mental and doesn't know what to do, so it creates a lot of pages with the 1 result on each page.
I found the answer to this issue.

Apparently the THEME_query_search_alter hook was triggered twice (also saw this happen when altering the standard Drupal user search query) and the new table was therefore joined into the final database query twice.

So when it tried to find results in the same table two times it kinda broke the search results, and returned these many many results instead of just a few.

The fix was then to check if the new table was already joined into the query, and this is how I did it (only changed the THEME_query_search_alter hook):

function hedeselskabet_query_search_alter(QueryAlterableInterface $query) {
    $keywords = $query-&gt;getMetaData('keywords');
    $words = "%" . $keywords . "%";

    if (!array_key_exists('n', $query-&gt;getTables())) {
        $or = db_or()-&gt;condition('title', $words, 'LIKE');
        $query-&gt;leftJoin('field_data_body', 'n', 'node.nid = n.entity_id');
        $or-&gt;condition('n.body_value', $words, 'LIKE');
        $query-&gt;condition($or);
    }
}


And now everything works. :)