Project

General

Profile

Feature #14179 ยป 0000115-mm_query.php

Administrator Admin, 2004-05-21 09:06

 
/**
* Creates a SELECT query for joining three tables according the the MM-relation standards used for tables configured in $TCA. That means MM-joins where the join table has the fields "uid_local" and "uid_foreign"
* Several mmtables and foreign tables can be passed via an array structure
*
* @param string List of fields to select
* @param string|array The local table
* @param string|array The join-table; The "uid_local" field of this table will be matched with $local_table's "uid" field.
* @param string|array Optionally: The foreign table; The "uid" field of this table will be matched with $mm_table's "uid_foreign" field. If you set this field to blank the join will be over only the $local_table and $mm_table
* @param string Optional endclause.
* @return string The query.
* @see mm_query_uidList()
*/
function mm_query($select,$local_table,$mm_table,$foreign_table,$endClause='') {
$query = 'SELECT '.$select.' FROM '.$local_table.',';
if ( is_array ( $mm_table ) ) {
// tables are passed as arrays of strings
$wherequery = ' WHERE';
$firsttime = true; // indicates, whether the loop is in the first round
foreach ( $mm_table as $mmtab ) {
$foreigntab = array_shift ( $foreign_table );
if ( !$firsttime ) {
$wherequery .= ' AND';
$query .= ',';
}
$query .= $mmtab.($foreigntab?','.$foreigntab:'');
$wherequery .= ' '.$local_table;
$wherequery .= '.uid='.$mmtab;
$wherequery .= '.uid_local';
$wherequery .= ($foreigntab?' AND '.$foreigntab.'.uid='.$mmtab.'.uid_foreign ':' ');
$firsttime = false;
}
$query .= $wherequery.$endClause;
}
else { // one string only
$query .= $mm_table.($foreign_table?','.$foreign_table:'');
$query .= ' WHERE '.$local_table.'.uid='.$mm_table.'.uid_local'.($foreign_table?' AND '.$foreign_table.'.uid='.$mm_table.'.uid_foreign ':' ').$endClause;
}

return $query;
}
    (1-1/1)