Commits

Jon Langevin committed 7874e28

Updated query builder to allow merging of built query parameters

  • Participants
  • Parent commits 91ce944

Comments (0)

Files changed (1)

File framework/db/CDbCommand.php

 	 * @since 1.1.6
 	 */
 	public $params=array();
+    public $autoMerge=false;
 
 	private $_connection;
 	private $_text;
 		return $sql;
 	}
 
+    public function toggleAutoMerge($enable=false) {
+        $this->autoMerge = $enable;
+        return $this;
+    }
+
 	/**
 	 * Sets the SELECT part of the query.
 	 * @param mixed $columns the columns to be selected. Defaults to '*', meaning all columns.
 	public function select($columns='*')
 	{
 		if(is_string($columns) && strpos($columns,'(')!==false)
-			$this->_query['select']=$columns;
+			$select=$columns;
 		else
 		{
 			if(!is_array($columns))
 						$columns[$i]=$this->_connection->quoteColumnName($column);
 				}
 			}
-			$this->_query['select']=implode(', ',$columns);
+			$select=implode(', ',$columns);
 		}
+        if(!empty($this->_query['select']) && $this->autoMerge)
+            $this->_query['select'] = $this->_query['select'] . ', ' . $select;
+        else
+            $this->_query['select'] = $select;
+
 		return $this;
 	}
 
 	public function from($tables)
 	{
 		if(is_string($tables) && strpos($tables,'(')!==false)
-			$this->_query['from']=$tables;
+			$from=$tables;
 		else
 		{
 			if(!is_array($tables))
 						$tables[$i]=$this->_connection->quoteTableName($table);
 				}
 			}
-			$this->_query['from']=implode(', ',$tables);
+			$from=implode(', ',$tables);
 		}
+        if(!empty($this->_query['from']) && $this->autoMerge)
+            $this->_query['from'] = $this->_query['from'] . ', ' . $from;
+        else
+            $this->_query['from'] = $from;
+
 		return $this;
 	}
 
 	 */
 	public function where($conditions, $params=array())
 	{
+        if(!empty($this->_query['where']) && $this->autoMerge)
+            $conditions=array('and',$this->_query['where'],$conditions);
 		$this->_query['where']=$this->processConditions($conditions);
 		foreach($params as $name=>$value)
 			$this->params[$name]=$value;
 	public function group($columns)
 	{
 		if(is_string($columns) && strpos($columns,'(')!==false)
-			$this->_query['group']=$columns;
+			$group=$columns;
 		else
 		{
 			if(!is_array($columns))
 				else if(strpos($column,'(')===false)
 					$columns[$i]=$this->_connection->quoteColumnName($column);
 			}
-			$this->_query['group']=implode(', ',$columns);
+			$group=implode(', ',$columns);
 		}
+        if(!empty($this->_query['group']) && $this->autoMerge)
+            $this->_query['group'] = $this->_query['group'] . ', ' . $group;
+        else
+            $this->_query['group'] = $group;
+
 		return $this;
 	}
 
 	 */
 	public function having($conditions, $params=array())
 	{
+        if(!empty($this->_query['having']) && $this->autoMerge)
+            $conditions=array('and',$this->_query['having'],$conditions);
 		$this->_query['having']=$this->processConditions($conditions);
 		foreach($params as $name=>$value)
 			$this->params[$name]=$value;
 	public function order($columns)
 	{
 		if(is_string($columns) && strpos($columns,'(')!==false)
-			$this->_query['order']=$columns;
+			$order=$columns;
 		else
 		{
 			if(!is_array($columns))
 						$columns[$i]=$this->_connection->quoteColumnName($column);
 				}
 			}
-			$this->_query['order']=implode(', ',$columns);
+			$order=implode(', ',$columns);
 		}
+        if(!empty($this->_query['order']) && $this->autoMerge)
+            $this->_query['order'] = $this->_query['order'] . ', ' . $order;
+        else
+            $this->_query['order'] = $order;
+
 		return $this;
 	}