public
Last active

Query Builder для Yii

  • Download Gist
Query Builder для Yii.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
public function buildQuery($query)
{
$sql=isset($query['distinct']) && $query['distinct'] ? 'SELECT DISTINCT' : 'SELECT';
$sql.=' '.(isset($query['select']) ? $query['select'] : '*');
if(isset($query['from']))
$sql.="\nFROM ".$query['from'];
else
throw new CDbException(Yii::t('yii','The DB query must contain the "from" portion.'));
if(isset($query['where']))
$sql.="\nWHERE ".$query['where'];
if(isset($query['join']))
$sql.="\n".(is_array($query['join']) ? implode("\n",$query['join']) : $query['join']);
if(isset($query['group']))
$sql.="\nGROUP BY ".$query['group'];
if(isset($query['having']))
$sql.="\nHAVING ".$query['having'];
if(isset($query['order']))
$sql.="\nORDER BY ".$query['order'];
$limit=isset($query['limit']) ? (int)$query['limit'] : -1;
$offset=isset($query['offset']) ? (int)$query['offset'] : -1;
if($limit>=0 && $offset>=0)
$sql=$this->_connection->getCommandBuilder()->applyLimit($sql,$limit,$offset);
if(isset($query['union']))
$sql.="\nUNION (\n".(is_array($query['union']) ? implode("\n) UNION (\n",$query['union']) : $query['union']) . ')';
return $sql;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.