public function join(
        $foreign_table,
        $master_field = null,
        $join_kind = null,
        $_foreign_alias = null
    ) {
        // Compatibility mode
        if (isset($this->app->compat)) {
            if (strpos($foreign_table, ' ')) {
                list($foreign_table, $alias) = explode(' ', $foreign_table);
                $foreign_table = array($alias => $foreign_table);
            }
            if (strpos($master_field, '=')) {
                $master_field = $this->expr($master_field);
            }
        }

        // If array - add recursively
        if (is_array($foreign_table)) {
            foreach ($foreign_table as $alias => $foreign) {
                if (is_numeric($alias)) {
                    $alias = null;
                }

                $this->join($foreign, $master_field, $join_kind, $alias);
            }

            return $this;
        }
        $j = array();

        // Split and deduce fields
        list($f1, $f2) = explode('.', $foreign_table, 2);

        if (is_object($master_field)) {
            $j['expr'] = $master_field;
        } else {
            // Split and deduce primary table
            if (is_null($master_field)) {
                list($m1, $m2) = array(null, null);
            } else {
                list($m1, $m2) = explode('.', $master_field, 2);
            }
            if (is_null($m2)) {
                $m2 = $m1;
                $m1 = null;
            }
            if (is_null($m1)) {
                $m1 = $this->main_table;
            }

            // Identify fields we use for joins
            if (is_null($f2) && is_null($m2)) {
                $m2 = $f1.'_id';
            }
            if (is_null($m2)) {
                $m2 = 'id';
            }
            $j['m1'] = $m1;
            $j['m2'] = $m2;
        }
        $j['f1'] = $f1;
        if (is_null($f2)) {
            $f2 = 'id';
        }
        $j['f2'] = $f2;

        $j['t'] = $join_kind ?: 'left';
        $j['fa'] = $_foreign_alias;

        $this->args['join'][] = $j;

        return $this;
    }