db_select() ignores condition() when used with join I want to select all rows from 'ha' table, which access column are >= 0 and node type of that nid is equal $this->ctype.

  protected function queryAlter() {

    if(isset($this-&gt;ctype)) {

        $this-&gt;query-&gt;leftJoin('node', 'n', 'n.nid = ha.nid');
        $this-&gt;query-&gt;conditions(
                db_and()
                -&gt;condition('ha.access', 0, '&gt;=')
                -&gt;condition('n.type', $this-&gt;ctype));
        //dpr($this-&gt;query);
    }
    else {
        $this-&gt;query-&gt;condition('ha.access', 0, '&gt;=');
    }
  }


$this->ctype is set and equal to 'ad'. And I have not any nodes with that type.
But db_select() ignores condition('n.type', $this->ctype) and returns all rows from ha (which NID's are not ad type).

Why this happens? And how to debug db_select() in general?
HearthbeatStream::$query is an object of the class PagerActivity which extend the class SelectQuery; this is evident from the code for HeartbeatStream::createQuery(), where the value of the property is initialized with the following line.

$this-&gt;query = db_select('heartbeat_activity', 'ha')-&gt;extend('PagerActivity');


The fact PagerActivity is an extender of SelectQuery means that every method implemented by SelectQuery is also implemented from PagerActivity.

SelectQuery::conditions() is a getter method, not a setter method. The only method to set the condition associated with a SelectQuery object is SelectQuery::condition().

The code should be re-written as:

  protected function queryAlter() {
    if (isset($this-&gt;ctype)) {
      $this-&gt;query-&gt;leftJoin('node', 'n', 'n.nid = ha.nid');
      $this-&gt;query-&gt;condition(('ha.access', 0, '&gt;=')
        -&gt;condition('n.type', $this-&gt;ctype));
    }
    else {
        $this-&gt;query-&gt;condition('ha.access', 0, '&gt;=');
    }
  }


To make a comparison, this is the code executed from blog_page_user():

  $query = db_select('node', 'n')-&gt;extend('PagerDefault');
  $nids = $query
    -&gt;fields('n', array('nid', 'sticky', 'created'))
    -&gt;condition('type', 'blog')
    -&gt;condition('uid', $account-&gt;uid)
    -&gt;condition('status', 1)
    -&gt;orderBy('sticky', 'DESC')
    -&gt;orderBy('created', 'DESC')
    -&gt;limit(variable_get('default_nodes_main', 10))
    -&gt;addTag('node_access')
    -&gt;execute()
    -&gt;fetchCol();


There are some similarities with your code:


in both the cases, the value returned from db_select() is passed to the extend() method
in both the cases, more than one condition is applied


Using the value returned from db_and() with SelectQuery::condition() is not necessary, as the constructor of SelectQuery initializes the value of SelectQuery::$where (used by SelectQuery::condition()) with the following code:

  $this-&gt;where = new DatabaseCondition('AND');


The returned value is the same returned by db_and().

function db_and() {
  return new DatabaseCondition('AND');
}