j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Multiple conditions in raw_join? #345

Closed fillibuster77 closed 5 years ago

fillibuster77 commented 5 years ago

Hi, is it possible to use multiple Join Conditions in an IDIORM query? I tried this, but it doesnt work:

raw_join(
    'LEFT JOIN addresses', 
    array(
        array('organisations.id', '=', 'address1.organisation_id'),
        array('address1.address_type_id', '=', '1')
    ),
    'address1'
);
treffynnon commented 5 years ago

This is not a supported syntax. Raw join gives you one condition. Note the constraint var in the source code from idiorm below.

 /**
         * Add a RAW JOIN source to the query
         */
        public function raw_join($table, $constraint, $table_alias, $parameters = array()) {
            // Add table alias if present
            if (!is_null($table_alias)) {
                $table_alias = $this->_quote_identifier($table_alias);
                $table .= " {$table_alias}";
            }
            $this->_values = array_merge($this->_values, $parameters);
            // Build the constraint
            if (is_array($constraint)) {
                list($first_column, $operator, $second_column) = $constraint;
                $first_column = $this->_quote_identifier($first_column);
                $second_column = $this->_quote_identifier($second_column);
                $constraint = "{$first_column} {$operator} {$second_column}";
            }
            $this->_join_sources[] = "{$table} ON {$constraint}";
            return $this;
        }

For more complex queries see the raw query docs https://idiorm.readthedocs.io/en/latest/querying.html#raw-queries