ORM::_build_select PHP Method

_build_select() protected method

Build a SELECT statement based on the clauses that have been passed to this instance by chaining method calls.
protected _build_select ( )
    protected function _build_select()
    {
        // If the query is raw, just set the $this->_values to be
        // the raw query parameters and return the raw query
        if ($this->_is_raw_query) {
            $this->_values = $this->_raw_parameters;
            return $this->_raw_query;
        }
        // Build and return the full SELECT statement by concatenating
        // the results of calling each separate builder method.
        return $this->_join_if_not_empty(" ", array($this->_build_select_start(), $this->_build_join(), $this->_build_where(), $this->_build_group_by(), $this->_build_having(), $this->_build_order_by(), $this->_build_limit(), $this->_build_offset()));
    }

Usage Example

 /**
  * This function only produces the COUNT query wrapped in `_dt_record_cnt`
  * The parent function produces the main select queries
  */
 protected function _build_select()
 {
     if (!$this->cnt_query) {
         return parent::_build_select();
     }
     // If the query is raw we need to remove the LIMIT and OFFSET and
     // replace the param values before proceeding.
     if ($this->_is_raw_query) {
         $this->_values = $this->_raw_parameters;
         $query = $this->_raw_query;
         $new_vals = array();
         if (is_array($this->_values)) {
             foreach ($this->_values as $k => $v) {
                 // @todo find a better way to do this for raw_queries
                 if ($k == 'limit') {
                     $query = str_ireplace('LIMIT :limit', '', $query);
                     continue;
                 }
                 if ($k == 'offset') {
                     $query = str_ireplace('OFFSET :offset', '', $query);
                     continue;
                 }
                 if (!is_numeric($v)) {
                     $v = '"' . $v . '"';
                 }
                 $query = str_replace(':' . $k, $v, $query);
             }
         }
         $query = "SELECT COUNT(*) as _dt_record_cnt FROM (" . $query . ") dt_bridge_cnt";
         return $query;
     }
     // Build and return the full SELECT statement by concatenating
     // the results of calling each separate builder method.
     $query = $this->_join_if_not_empty(" ", array($this->_build_select_start(), $this->_build_join(), $this->_build_where(), $this->_build_group_by(), $this->_build_having(), $this->_build_order_by(), $this->_build_limit(), $this->_build_offset()));
     $query = "SELECT COUNT(*) as _dt_record_cnt FROM (" . $query . ") dt_bridge_cnt";
     return $query;
 }
ORM