CDbCommandBuilder

优质
小牛编辑
128浏览
2023-12-01
所有包 | 属性 | 方法
system.db.schema
继承class CDbCommandBuilder » CComponent
子类CMssqlCommandBuilder, COciCommandBuilder, CSqliteCommandBuilder
源自1.0
版本$Id: CDbCommandBuilder.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/db/schema/CDbCommandBuilder.php
CDbCommandBuilder provides basic methods to create query commands for tables.

公共属性

隐藏继承属性

属性类型描述定义在
dbConnectionCDbConnectiondatabase connection.CDbCommandBuilder
schemaCDbSchemathe schema for this command builder.CDbCommandBuilder

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()CDbCommandBuilder
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
applyCondition()Alters the SQL to apply WHERE clause.CDbCommandBuilder
applyGroup()Alters the SQL to apply GROUP BY.CDbCommandBuilder
applyHaving()Alters the SQL to apply HAVING.CDbCommandBuilder
applyJoin()Alters the SQL to apply JOIN clause.CDbCommandBuilder
applyLimit()Alters the SQL to apply LIMIT and OFFSET.CDbCommandBuilder
applyOrder()Alters the SQL to apply ORDER BY.CDbCommandBuilder
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
bindValues()Binds parameter values for an SQL command.CDbCommandBuilder
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
createColumnCriteria()Creates a query criteria with the specified column values.CDbCommandBuilder
createCountCommand()Creates a COUNT(*) command for a single table.CDbCommandBuilder
createCriteria()Creates a query criteria.CDbCommandBuilder
createDeleteCommand()Creates a DELETE command.CDbCommandBuilder
createFindCommand()Creates a SELECT command for a single table.CDbCommandBuilder
createInCondition()Generates the expression for selecting rows of specified primary key values.CDbCommandBuilder
createInsertCommand()Creates an INSERT command.CDbCommandBuilder
createPkCondition()Generates the expression for selecting rows of specified primary key values.CDbCommandBuilder
createPkCriteria()Creates a query criteria with the specified primary key.CDbCommandBuilder
createSearchCondition()Generates the expression for searching the specified keywords within a list of columns.CDbCommandBuilder
createSqlCommand()Creates a command based on a given SQL statement.CDbCommandBuilder
createUpdateCommand()Creates an UPDATE command.CDbCommandBuilder
createUpdateCounterCommand()Creates an UPDATE command that increments/decrements certain columns.CDbCommandBuilder
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getDbConnection()返回database connection.CDbCommandBuilder
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getLastInsertID()Returns the last insertion ID for the specified table.CDbCommandBuilder
getSchema()返回the schema for this command builder.CDbCommandBuilder
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
raiseEvent()发起一个事件。CComponent

受保护方法

隐藏继承方法

方法描述定义在
createCompositeInCondition()Generates the expression for selecting rows with specified composite key values.CDbCommandBuilder
ensureTable()Checks if the parameter is a valid table schema.CDbCommandBuilder

属性详细

dbConnection 属性 只读 public CDbConnection getDbConnection()

database connection.

schema 属性 只读 public CDbSchema getSchema()

the schema for this command builder.

方法详细

__construct() 方法
public void __construct(CDbSchema $schema)
$schemaCDbSchemathe schema for this command builder
源码: framework/db/schema/CDbCommandBuilder.php#32 (显示) publicfunction__construct($schema)
{
$this->_schema=$schema;
$this->_connection=$schema->getDbConnection();
}
applyCondition() 方法
public string applyCondition(string $sql, string $condition)
$sqlstringthe SQL statement without WHERE clause
$conditionstringthe WHERE clause (without WHERE keyword)
{return}stringthe altered SQL statement
源码: framework/db/schema/CDbCommandBuilder.php#355 (显示) publicfunctionapplyCondition($sql,$condition)
{
if($condition!='')
return$sql.'WHERE'.$condition;
else
return$sql;
}

Alters the SQL to apply WHERE clause.

applyGroup() 方法
public string applyGroup(string $sql, string $group)
$sqlstringSQL query string without GROUP BY.
$groupstringGROUP BY
{return}stringSQL with GROUP BY.
源码: framework/db/schema/CDbCommandBuilder.php#400 (显示) publicfunctionapplyGroup($sql,$group)
{
if($group!='')
return$sql.'GROUPBY'.$group;
else
return$sql;
}

Alters the SQL to apply GROUP BY.

applyHaving() 方法
public string applyHaving(string $sql, string $having)
$sqlstringSQL query string without HAVING
$havingstringHAVING
{return}stringSQL with HAVING
源码: framework/db/schema/CDbCommandBuilder.php#414 (显示) publicfunctionapplyHaving($sql,$having)
{
if($having!='')
return$sql.'HAVING'.$having;
else
return$sql;
}

Alters the SQL to apply HAVING.

applyJoin() 方法
public string applyJoin(string $sql, string $join)
$sqlstringthe SQL statement to be altered
$joinstringthe JOIN clause (starting with join type, such as INNER JOIN)
{return}stringthe altered SQL statement
源码: framework/db/schema/CDbCommandBuilder.php#341 (显示) publicfunctionapplyJoin($sql,$join)
{
if($join!='')
return$sql.''.$join;
else
return$sql;
}

Alters the SQL to apply JOIN clause.

applyLimit() 方法
public string applyLimit(string $sql, integer $limit, integer $offset)
$sqlstringSQL query string without LIMIT and OFFSET.
$limitintegermaximum number of rows, -1 to ignore limit.
$offsetintegerrow offset, -1 to ignore offset.
{return}stringSQL with LIMIT and OFFSET
源码: framework/db/schema/CDbCommandBuilder.php#385 (显示) publicfunctionapplyLimit($sql,$limit,$offset)
{
if($limit>=0)
$sql.='LIMIT'.(int)$limit;
if($offset>0)
$sql.='OFFSET'.(int)$offset;
return$sql;
}

Alters the SQL to apply LIMIT and OFFSET. Default implementation is applicable for PostgreSQL, MySQL and SQLite.

applyOrder() 方法
public string applyOrder(string $sql, string $orderBy)
$sqlstringSQL statement without ORDER BY.
$orderBystringcolumn ordering
{return}stringmodified SQL applied with ORDER BY.
源码: framework/db/schema/CDbCommandBuilder.php#369 (显示) publicfunctionapplyOrder($sql,$orderBy)
{
if($orderBy!='')
return$sql.'ORDERBY'.$orderBy;
else
return$sql;
}

Alters the SQL to apply ORDER BY.

bindValues() 方法
public void bindValues(CDbCommand $command, array $values)
$commandCDbCommanddatabase command
$valuesarrayvalues for binding (integer-indexed array for question mark placeholders, string-indexed array for named placeholders)
源码: framework/db/schema/CDbCommandBuilder.php#427 (显示) publicfunctionbindValues($command,$values)
{
if(($n=count($values))===0)
return;
if(isset($values[0]))//questionmarkplaceholders
{
for($i=0;$i<$n;++$i)
$command->bindValue($i+1,$values[$i]);
}
else//namedplaceholders
{
foreach($valuesas$name=>$value)
{
if($name[0]!==':')
$name=':'.$name;
$command->bindValue($name,$value);
}
}
}

Binds parameter values for an SQL command.

createColumnCriteria() 方法
public CDbCriteria createColumnCriteria(mixed $table, array $columns, mixed $condition='', array $params=array ( ), string $prefix=NULL)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$columnsarraycolumn values that should be matched in the query (name=>value)
$conditionmixedquery condition or criteria. If a string, it is treated as query condition; If an array, it is treated as the initial values for constructing a CDbCriteria; Otherwise, it should be an instance of CDbCriteria.
$paramsarrayparameters to be bound to an SQL statement. This is only used when the third parameter is a string (query condition). In other cases, please use CDbCriteria::params to set parameters.
$prefixstringcolumn prefix (ended with dot). If null, it will be the table name
{return}CDbCriteriathe created query criteria
源码: framework/db/schema/CDbCommandBuilder.php#534 (显示) publicfunctioncreateColumnCriteria($table,$columns,$condition='',$params=array(),$prefix=null)
{
$this->ensureTable($table);
$criteria=$this->createCriteria($condition,$params);
if($criteria->alias!='')
$prefix=$this->_schema->quoteTableName($criteria->alias).'.';
$bindByPosition=isset($criteria->params[0]);
$conditions=array();
$values=array();
$i=0;
if($prefix===null)
$prefix=$table->rawName.'.';
foreach($columnsas$name=>$value)
{
if(($column=$table->getColumn($name))!==null)
{
if(is_array($value))
$conditions[]=$this->createInCondition($table,$name,$value,$prefix);
elseif($value!==null)
{
if($bindByPosition)
{
$conditions[]=$prefix.$column->rawName.'=?';
$values[]=$value;
}
else
{
$conditions[]=$prefix.$column->rawName.'='.self::PARAM_PREFIX.$i;
$values[self::PARAM_PREFIX.$i]=$value;
$i++;
}
}
else
$conditions[]=$prefix.$column->rawName.'ISNULL';
}
else
thrownewCDbException(Yii::t('yii','Table"{table}"doesnothaveacolumnnamed"{column}".',
array('{table}'=>$table->name,'{column}'=>$name)));
}
$criteria->params=array_merge($values,$criteria->params);
if(isset($conditions[0]))
{
if($criteria->condition!='')
$criteria->condition=implode('AND',$conditions).'AND('.$criteria->condition.')';
else
$criteria->condition=implode('AND',$conditions);
}
return$criteria;
}

Creates a query criteria with the specified column values.

createCompositeInCondition() 方法
protected string createCompositeInCondition(CDbTableSchema $table, array $values, string $prefix)
$tableCDbTableSchemathe table schema
$valuesarraylist of primary key values to be selected within
$prefixstringcolumn prefix (ended with dot)
{return}stringthe expression for selection
源码: framework/db/schema/CDbCommandBuilder.php#711 (显示) protectedfunctioncreateCompositeInCondition($table,$values,$prefix)
{
$keyNames=array();
foreach(array_keys($values[0])as$name)
$keyNames[]=$prefix.$table->columns[$name]->rawName;
$vs=array();
foreach($valuesas$value)
$vs[]='('.implode(',',$value).')';
return'('.implode(',',$keyNames).')IN('.implode(',',$vs).')';
}

Generates the expression for selecting rows with specified composite key values.

createCountCommand() 方法
public CDbCommand createCountCommand(mixed $table, CDbCriteria $criteria, string $alias='t')
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$criteriaCDbCriteriathe query criteria
$aliasstringthe alias name of the primary table. Defaults to 't'.
{return}CDbCommandquery command.
源码: framework/db/schema/CDbCommandBuilder.php#112 (显示) publicfunctioncreateCountCommand($table,$criteria,$alias='t')
{
$this->ensureTable($table);
if($criteria->alias!='')
$alias=$criteria->alias;
$alias=$this->_schema->quoteTableName($alias);

if(!empty($criteria->group)||!empty($criteria->having))
{
$select=is_array($criteria->select)?implode(',',$criteria->select):$criteria->select;
if($criteria->alias!='')
$alias=$criteria->alias;
$sql=($criteria->distinct?'SELECTDISTINCT':'SELECT')."{$select}FROM{$table->rawName}$alias";
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
$sql=$this->applyGroup($sql,$criteria->group);
$sql=$this->applyHaving($sql,$criteria->having);
$sql="SELECTCOUNT(*)FROM($sql)sq";
}
else
{
if(is_string($criteria->select)&&stripos($criteria->select,'count')===0)
$sql="SELECT".$criteria->select;
elseif($criteria->distinct)
{
if(is_array($table->primaryKey))
{
$pk=array();
foreach($table->primaryKeyas$key)
$pk[]=$alias.'.'.$key;
$pk=implode(',',$pk);
}
else
$pk=$alias.'.'.$table->primaryKey;
$sql="SELECTCOUNT(DISTINCT$pk)";
}
else
$sql="SELECTCOUNT(*)";
$sql.="FROM{$table->rawName}$alias";
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
}

$command=$this->_connection->createCommand($sql);
$this->bindValues($command,$criteria->params);
return$command;
}

Creates a COUNT(*) command for a single table.

createCriteria() 方法
public CDbCriteria createCriteria(mixed $condition='', array $params=array ( ))
$conditionmixedquery condition or criteria. If a string, it is treated as query condition (the WHERE clause); If an array, it is treated as the initial values for constructing a CDbCriteria object; Otherwise, it should be an instance of CDbCriteria.
$paramsarrayparameters to be bound to an SQL statement. This is only used when the first parameter is a string (query condition). In other cases, please use CDbCriteria::params to set parameters.
{return}CDbCriteriathe created query criteria
源码: framework/db/schema/CDbCommandBuilder.php#459 (显示) publicfunctioncreateCriteria($condition='',$params=array())
{
if(is_array($condition))
$criteria=newCDbCriteria($condition);
elseif($conditioninstanceofCDbCriteria)
$criteria=clone$condition;
else
{
$criteria=newCDbCriteria;
$criteria->condition=$condition;
$criteria->params=$params;
}
return$criteria;
}

Creates a query criteria.

createDeleteCommand() 方法
public CDbCommand createDeleteCommand(mixed $table, CDbCriteria $criteria)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$criteriaCDbCriteriathe query criteria
{return}CDbCommanddelete command.
源码: framework/db/schema/CDbCommandBuilder.php#166 (显示) publicfunctioncreateDeleteCommand($table,$criteria)
{
$this->ensureTable($table);
$sql="DELETEFROM{$table->rawName}";
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
$sql=$this->applyGroup($sql,$criteria->group);
$sql=$this->applyHaving($sql,$criteria->having);
$sql=$this->applyOrder($sql,$criteria->order);
$sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset);
$command=$this->_connection->createCommand($sql);
$this->bindValues($command,$criteria->params);
return$command;
}

Creates a DELETE command.

createFindCommand() 方法
public CDbCommand createFindCommand(mixed $table, CDbCriteria $criteria, string $alias='t')
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$criteriaCDbCriteriathe query criteria
$aliasstringthe alias name of the primary table. Defaults to 't'.
{return}CDbCommandquery command.
源码: framework/db/schema/CDbCommandBuilder.php#75 (显示) publicfunctioncreateFindCommand($table,$criteria,$alias='t')
{
$this->ensureTable($table);
$select=is_array($criteria->select)?implode(',',$criteria->select):$criteria->select;
if($criteria->alias!='')
$alias=$criteria->alias;
$alias=$this->_schema->quoteTableName($alias);

//issue1432:needtoexpand*whenSQLhasJOIN
if($select==='*'&&!empty($criteria->join))
{
$prefix=$alias.'.';
$select=array();
foreach($table->getColumnNames()as$name)
$select[]=$prefix.$this->_schema->quoteColumnName($name);
$select=implode(',',$select);
}

$sql=($criteria->distinct?'SELECTDISTINCT':'SELECT')."{$select}FROM{$table->rawName}$alias";
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
$sql=$this->applyGroup($sql,$criteria->group);
$sql=$this->applyHaving($sql,$criteria->having);
$sql=$this->applyOrder($sql,$criteria->order);
$sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset);
$command=$this->_connection->createCommand($sql);
$this->bindValues($command,$criteria->params);
return$command;
}

Creates a SELECT command for a single table.

createInCondition() 方法
public string createInCondition(mixed $table, mixed $columnName, array $values, string $prefix=NULL)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$columnNamemixedthe column name(s). It can be either a string indicating a single column or an array of column names. If the latter, it stands for a composite key.
$valuesarraylist of key values to be selected within
$prefixstringcolumn prefix (ended with dot). If null, it will be the table name
{return}stringthe expression for selection
源码: framework/db/schema/CDbCommandBuilder.php#634 (显示) publicfunctioncreateInCondition($table,$columnName,$values,$prefix=null)
{
if(($n=count($values))<1)
return'0=1';

$this->ensureTable($table);

if($prefix===null)
$prefix=$table->rawName.'.';

$db=$this->_connection;

if(is_array($columnName)&&count($columnName)===1)
$columnName=reset($columnName);

if(is_string($columnName))//simplekey
{
if(!isset($table->columns[$columnName]))
thrownewCDbException(Yii::t('yii','Table"{table}"doesnothaveacolumnnamed"{column}".',
array('{table}'=>$table->name,'{column}'=>$columnName)));
$column=$table->columns[$columnName];

foreach($valuesas&$value)
{
$value=$column->typecast($value);
if(is_string($value))
$value=$db->quoteValue($value);
}
if($n===1)
return$prefix.$column->rawName.($values[0]===null?'ISNULL':'='.$values[0]);
else
return$prefix.$column->rawName.'IN('.implode(',',$values).')';
}
elseif(is_array($columnName))//compositekey:$values=array(array('pk1'=>'v1','pk2'=>'v2'),array(...))
{
foreach($columnNameas$name)
{
if(!isset($table->columns[$name]))
thrownewCDbException(Yii::t('yii','Table"{table}"doesnothaveacolumnnamed"{column}".',
array('{table}'=>$table->name,'{column}'=>$name)));

for($i=0;$i<$n;++$i)
{
if(isset($values[$i][$name]))
{
$value=$table->columns[$name]->typecast($values[$i][$name]);
if(is_string($value))
$values[$i][$name]=$db->quoteValue($value);
else
$values[$i][$name]=$value;
}
else
thrownewCDbException(Yii::t('yii','Thevalueforthecolumn"{column}"isnotsuppliedwhenqueryingthetable"{table}".',
array('{table}'=>$table->name,'{column}'=>$name)));
}
}
if(count($values)===1)
{
$entries=array();
foreach($values[0]as$name=>$value)
$entries[]=$prefix.$table->columns[$name]->rawName.($value===null?'ISNULL':'='.$value);
returnimplode('AND',$entries);
}

return$this->createCompositeInCondition($table,$values,$prefix);
}
else
thrownewCDbException(Yii::t('yii','Columnnamemustbeeitherastringoranarray.'));
}

Generates the expression for selecting rows of specified primary key values.

createInsertCommand() 方法
public CDbCommand createInsertCommand(mixed $table, array $data)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$dataarraydata to be inserted (column name=>column value). If a key is not a valid column name, the corresponding value will be ignored.
{return}CDbCommandinsert command
源码: framework/db/schema/CDbCommandBuilder.php#187 (显示) publicfunctioncreateInsertCommand($table,$data)
{
$this->ensureTable($table);
$fields=array();
$values=array();
$placeholders=array();
$i=0;
foreach($dataas$name=>$value)
{
if(($column=$table->getColumn($name))!==null&&($value!==null||$column->allowNull))
{
$fields[]=$column->rawName;
if($valueinstanceofCDbExpression)
{
$placeholders[]=$value->expression;
foreach($value->paramsas$n=>$v)
$values[$n]=$v;
}
else
{
$placeholders[]=self::PARAM_PREFIX.$i;
$values[self::PARAM_PREFIX.$i]=$column->typecast($value);
$i++;
}
}
}
if($fields===array())
{
$pks=is_array($table->primaryKey)?$table->primaryKey:array($table->primaryKey);
foreach($pksas$pk)
{
$fields[]=$table->getColumn($pk)->rawName;
$placeholders[]='NULL';
}
}
$sql="INSERTINTO{$table->rawName}(".implode(',',$fields).')VALUES('.implode(',',$placeholders).')';
$command=$this->_connection->createCommand($sql);

foreach($valuesas$name=>$value)
$command->bindValue($name,$value);

return$command;
}

Creates an INSERT command.

createPkCondition() 方法
public string createPkCondition(mixed $table, array $values, string $prefix=NULL)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$valuesarraylist of primary key values to be selected within
$prefixstringcolumn prefix (ended with dot). If null, it will be the table name
{return}stringthe expression for selection
源码: framework/db/schema/CDbCommandBuilder.php#514 (显示) publicfunctioncreatePkCondition($table,$values,$prefix=null)
{
$this->ensureTable($table);
return$this->createInCondition($table,$table->primaryKey,$values,$prefix);
}

Generates the expression for selecting rows of specified primary key values.

createPkCriteria() 方法
public CDbCriteria createPkCriteria(mixed $table, mixed $pk, mixed $condition='', array $params=array ( ), string $prefix=NULL)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$pkmixedprimary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
$conditionmixedquery condition or criteria. If a string, it is treated as query condition; If an array, it is treated as the initial values for constructing a CDbCriteria; Otherwise, it should be an instance of CDbCriteria.
$paramsarrayparameters to be bound to an SQL statement. This is only used when the second parameter is a string (query condition). In other cases, please use CDbCriteria::params to set parameters.
$prefixstringcolumn prefix (ended with dot). If null, it will be the table name
{return}CDbCriteriathe created query criteria
源码: framework/db/schema/CDbCommandBuilder.php#488 (显示) publicfunctioncreatePkCriteria($table,$pk,$condition='',$params=array(),$prefix=null)
{
$this->ensureTable($table);
$criteria=$this->createCriteria($condition,$params);
if($criteria->alias!='')
$prefix=$this->_schema->quoteTableName($criteria->alias).'.';
if(!is_array($pk))//singlekey
$pk=array($pk);
if(is_array($table->primaryKey)&&!isset($pk[0])&&$pk!==array())//singlecompositekey
$pk=array($pk);
$condition=$this->createInCondition($table,$table->primaryKey,$pk,$prefix);
if($criteria->condition!='')
$criteria->condition=$condition.'AND('.$criteria->condition.')';
else
$criteria->condition=$condition;

return$criteria;
}

Creates a query criteria with the specified primary key.

createSearchCondition() 方法
public string createSearchCondition(mixed $table, array $columns, mixed $keywords, string $prefix=NULL, boolean $caseSensitive=true)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$columnsarraylist of column names for potential search condition.
$keywordsmixedsearch keywords. This can be either a string with space-separated keywords or an array of keywords.
$prefixstringoptional column prefix (with dot at the end). If null, the table name will be used as the prefix.
$caseSensitivebooleanwhether the search is case-sensitive. Defaults to true.
{return}stringSQL search condition matching on a set of columns. An empty string is returned if either the column array or the keywords are empty.
源码: framework/db/schema/CDbCommandBuilder.php#596 (显示) publicfunctioncreateSearchCondition($table,$columns,$keywords,$prefix=null,$caseSensitive=true)
{
$this->ensureTable($table);
if(!is_array($keywords))
$keywords=preg_split('/s+/u',$keywords,-1,PREG_SPLIT_NO_EMPTY);
if(empty($keywords))
return'';
if($prefix===null)
$prefix=$table->rawName.'.';
$conditions=array();
foreach($columnsas$name)
{
if(($column=$table->getColumn($name))===null)
thrownewCDbException(Yii::t('yii','Table"{table}"doesnothaveacolumnnamed"{column}".',
array('{table}'=>$table->name,'{column}'=>$name)));
$condition=array();
foreach($keywordsas$keyword)
{
$keyword='%'.strtr($keyword,array('%'=>'%','_'=>'_')).'%';
if($caseSensitive)
$condition[]=$prefix.$column->rawName.'LIKE'.$this->_connection->quoteValue('%'.$keyword.'%');
else
$condition[]='LOWER('.$prefix.$column->rawName.')LIKELOWER('.$this->_connection->quoteValue('%'.$keyword.'%').')';
}
$conditions[]=implode('AND',$condition);
}
return'('.implode('OR',$conditions).')';
}

Generates the expression for searching the specified keywords within a list of columns. The search expression is generated using the 'LIKE' SQL syntax. Every word in the keywords must be present and appear in at least one of the columns.

createSqlCommand() 方法
public CDbCommand createSqlCommand(string $sql, array $params=array ( ))
$sqlstringthe explicitly specified SQL statement
$paramsarrayparameters that will be bound to the SQL statement
{return}CDbCommandthe created command
源码: framework/db/schema/CDbCommandBuilder.php#328 (显示) publicfunctioncreateSqlCommand($sql,$params=array())
{
$command=$this->_connection->createCommand($sql);
$this->bindValues($command,$params);
return$command;
}

Creates a command based on a given SQL statement.

createUpdateCommand() 方法
public CDbCommand createUpdateCommand(mixed $table, array $data, CDbCriteria $criteria)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$dataarraylist of columns to be updated (name=>value)
$criteriaCDbCriteriathe query criteria
{return}CDbCommandupdate command.
源码: framework/db/schema/CDbCommandBuilder.php#238 (显示) publicfunctioncreateUpdateCommand($table,$data,$criteria)
{
$this->ensureTable($table);
$fields=array();
$values=array();
$bindByPosition=isset($criteria->params[0]);
$i=0;
foreach($dataas$name=>$value)
{
if(($column=$table->getColumn($name))!==null)
{
if($valueinstanceofCDbExpression)
{
$fields[]=$column->rawName.'='.$value->expression;
foreach($value->paramsas$n=>$v)
$values[$n]=$v;
}
elseif($bindByPosition)
{
$fields[]=$column->rawName.'=?';
$values[]=$column->typecast($value);
}
else
{
$fields[]=$column->rawName.'='.self::PARAM_PREFIX.$i;
$values[self::PARAM_PREFIX.$i]=$column->typecast($value);
$i++;
}
}
}
if($fields===array())
thrownewCDbException(Yii::t('yii','Nocolumnsarebeingupdatedfortable"{table}".',
array('{table}'=>$table->name)));
$sql="UPDATE{$table->rawName}SET".implode(',',$fields);
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
$sql=$this->applyOrder($sql,$criteria->order);
$sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset);

$command=$this->_connection->createCommand($sql);
$this->bindValues($command,array_merge($values,$criteria->params));

return$command;
}

Creates an UPDATE command.

createUpdateCounterCommand() 方法
public CDbCommand createUpdateCounterCommand(mixed $table, array $counters, CDbCriteria $criteria)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
$countersarraycounters to be updated (counter increments/decrements indexed by column names.)
$criteriaCDbCriteriathe query criteria
{return}CDbCommandthe created command
源码: framework/db/schema/CDbCommandBuilder.php#291 (显示) publicfunctioncreateUpdateCounterCommand($table,$counters,$criteria)
{
$this->ensureTable($table);
$fields=array();
foreach($countersas$name=>$value)
{
if(($column=$table->getColumn($name))!==null)
{
$value=(int)$value;
if($value<0)
$fields[]="{$column->rawName}={$column->rawName}-".(-$value);
else
$fields[]="{$column->rawName}={$column->rawName}+".$value;
}
}
if($fields!==array())
{
$sql="UPDATE{$table->rawName}SET".implode(',',$fields);
$sql=$this->applyJoin($sql,$criteria->join);
$sql=$this->applyCondition($sql,$criteria->condition);
$sql=$this->applyOrder($sql,$criteria->order);
$sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset);
$command=$this->_connection->createCommand($sql);
$this->bindValues($command,$criteria->params);
return$command;
}
else
thrownewCDbException(Yii::t('yii','Nocountercolumnsarebeingupdatedfortable"{table}".',
array('{table}'=>$table->name)));
}

Creates an UPDATE command that increments/decrements certain columns.

ensureTable() 方法
protected void ensureTable(mixed &$table)
$tablemixedtable schema (CDbTableSchema) or table name (string). If this refers to a valid table name, this parameter will be returned with the corresponding table schema.
源码: framework/db/schema/CDbCommandBuilder.php#729 (显示) protectedfunctionensureTable(&$table)
{
if(is_string($table)&&($table=$this->_schema->getTable($tableName=$table))===null)
thrownewCDbException(Yii::t('yii','Table"{table}"doesnotexist.',
array('{table}'=>$tableName)));
}

Checks if the parameter is a valid table schema. If it is a string, the corresponding table schema will be retrieved.

getDbConnection() 方法
public CDbConnection getDbConnection()
{return}CDbConnectiondatabase connection.
源码: framework/db/schema/CDbCommandBuilder.php#41 (显示) publicfunctiongetDbConnection()
{
return$this->_connection;
}
getLastInsertID() 方法
public mixed getLastInsertID(mixed $table)
$tablemixedthe table schema (CDbTableSchema) or the table name (string).
{return}mixedlast insertion id. Null is returned if no sequence name.
源码: framework/db/schema/CDbCommandBuilder.php#59 (显示) publicfunctiongetLastInsertID($table)
{
$this->ensureTable($table);
if($table->sequenceName!==null)
return$this->_connection->getLastInsertID($table->sequenceName);
else
returnnull;
}

Returns the last insertion ID for the specified table.

getSchema() 方法
public CDbSchema getSchema()
{return}CDbSchemathe schema for this command builder.
源码: framework/db/schema/CDbCommandBuilder.php#49 (显示) publicfunctiongetSchema()
{
return$this->_schema;
}