CI_DB_query_builder::join PHP Method

join() public method

Generates the JOIN portion of the query
public join ( $table, $cond, $type = '', $escape = NULL ) : CI_DB_query_builder
return CI_DB_query_builder
    public function join($table, $cond, $type = '', $escape = NULL)
    {
        if ($type !== '') {
            $type = strtoupper(trim($type));
            if (!in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE)) {
                $type = '';
            } else {
                $type .= ' ';
            }
        }
        // Extract any aliases that might exist. We use this information
        // in the protect_identifiers to know whether to add a table prefix
        $this->_track_aliases($table);
        is_bool($escape) or $escape = $this->_protect_identifiers;
        if (!$this->_has_operator($cond)) {
            $cond = ' USING (' . ($escape ? $this->escape_identifiers($cond) : $cond) . ')';
        } elseif ($escape === FALSE) {
            $cond = ' ON ' . $cond;
        } else {
            // Split multiple conditions
            if (preg_match_all('/\\sAND\\s|\\sOR\\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
                $conditions = array();
                $joints = $joints[0];
                array_unshift($joints, array('', 0));
                for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--) {
                    $joints[$i][1] += strlen($joints[$i][0]);
                    // offset
                    $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
                    $pos = $joints[$i][1] - strlen($joints[$i][0]);
                    $joints[$i] = $joints[$i][0];
                }
            } else {
                $conditions = array($cond);
                $joints = array('');
            }
            $cond = ' ON ';
            for ($i = 0, $c = count($conditions); $i < $c; $i++) {
                $operator = $this->_get_operator($conditions[$i]);
                $cond .= $joints[$i];
                $cond .= preg_match("/(\\(*)?([\\[\\]\\w\\.'-]+)" . preg_quote($operator) . "(.*)/i", $conditions[$i], $match) ? $match[1] . $this->protect_identifiers($match[2]) . $operator . $this->protect_identifiers($match[3]) : $conditions[$i];
            }
        }
        // Do we want to escape the table name?
        if ($escape === TRUE) {
            $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
        }
        // Assemble the JOIN statement
        $this->qb_join[] = $join = $type . 'JOIN ' . $table . $cond;
        if ($this->qb_caching === TRUE) {
            $this->qb_cache_join[] = $join;
            $this->qb_cache_exists[] = 'join';
        }
        return $this;
    }