CDbCommand

优质
小牛编辑
126浏览
2023-12-01
所有包 | 属性 | 方法
system.db
继承class CDbCommand » CComponent
源自1.0
版本$Id: CDbCommand.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/db/CDbCommand.php
CDbCommand表示一个针对数据库执行的SQL语句。

它通常通过调用CDbConnection::createCommand创建。 要执行的SQL语句可以通过Text设置。

要执行一条非查询的SQL(如:insert,delete,update),调用execute。 要执行一条SQL语句返回结果数据集(如:SELECT), 使用 query 或它的简便版本 queryRow, queryColumn或者queryScalar。

如果一个SQL语句返回结果(比如一个SELECT语句), 结果可以通过已返回的CDbDataReader访问。

CDbCommand支持SQL语句预处理和参数绑定。 调用 bindParam 去绑定一个PHP变量到SQL中的一个参数。 调用 bindValue 去绑定一个值到一个SQL参数。 当绑定一个参数时,此SQL语句将自动准备好。 你也可以调用prepare去明确的准备一条SQL语句。

开始自版本 1.1.6,CDbCommand也能用作一个查询生成器, 从代码片断建立一条SQL语句。例如:
$user = Yii::app()->db->createCommand()  ->select('username, password')  ->from('tbl_user')  ->where('id=:id', array(':id'=>1))  ->queryRow();

公共属性

隐藏继承属性

属性类型描述定义在
connectionCDbConnection和这个命令相关的连接。CDbCommand
distinctboolean返回一个值指示是否SELECT DISTINCT应该被使用。CDbCommand
fromstring返回查询的 FROM 部分。CDbCommand
groupstring返回查询中的 GROUP BY 部分。CDbCommand
havingstring返回查询的HAVING部分。CDbCommand
joinmixed返回查询的join部分。CDbCommand
limitstring返回查询的 LIMIT 部分。CDbCommand
offsetstring返回查询的 OFFSET 部分。CDbCommand
orderstring返回查询的 ORDER BY 部分。CDbCommand
paramsarray参数(name=>value)被绑定要查询的当前语句。CDbCommand
pdoStatementPDOStatement这个命令底层的PDOStatement 如果尚未准备好语句,它可以是null。CDbCommand
selectstring返回查询的SELECT部分。CDbCommand
textstring返回将被执行的SQL语句。CDbCommand
unionmixed返回查询的 UNION 部分。CDbCommand
wherestring返回查询的WHERE部分。CDbCommand

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造函数。CDbCommand
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__sleep()当序列化时语句声明为null。CDbCommand
__unset()设置一个组件的属性为null。CComponent
addColumn()绑定并执行一条SQL语句为增加一个新的数据列。CDbCommand
addForeignKey()为已经存在的数据表创建一条外键约束的SQL语句。CDbCommand
alterColumn()绑定并执行一条SQL语句为改变一个列的定义。CDbCommand
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
bindParam()绑定一个参数到要执行的语句。CDbCommand
bindValue()绑定一个值到参数。CDbCommand
bindValues()将一个列表的值绑定到相应参数。CDbCommand
buildQuery()生成一个SQL SELECT语句从给定查询规范。CDbCommand
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
cancel()取消SQL语句的执行。CDbCommand
createIndex()绑定并执行一条SQL语句为创建一个新索引。CDbCommand
createTable()绑定并执行一条SQL语句为创建一个数据表。CDbCommand
crossJoin()附加一个CROSS JOIN部分到此查询。CDbCommand
delete()创建并执行一条 DELETE SQL 语句。CDbCommand
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
dropColumn()绑定并执行一条SQL语句为删除一个数据列。CDbCommand
dropForeignKey()建立一条SQL语句为删除一个外键约束。CDbCommand
dropIndex()绑定并执行一条SQL语句为删除一个索引。CDbCommand
dropTable()绑定并执行一条SQL语句为删除一个数据表CDbCommand
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
execute()执行此 SQL 语句。CDbCommand
from()设置查询的FROM部分。CDbCommand
getConnection()返回和这个命令相关的连接。CDbCommand
getDistinct()返回一个值指示是否SELECT DISTINCT应该被使用。CDbCommand
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getFrom()返回查询的 FROM 部分。CDbCommand
getGroup()返回查询中的 GROUP BY 部分。CDbCommand
getHaving()返回查询的HAVING部分。CDbCommand
getJoin()返回查询的join部分。CDbCommand
getLimit()返回查询的 LIMIT 部分。CDbCommand
getOffset()返回查询的 OFFSET 部分。CDbCommand
getOrder()返回查询的 ORDER BY 部分。CDbCommand
getPdoStatement()返回这个命令底层的PDOStatement 如果尚未准备好语句,它可以是null。CDbCommand
getSelect()返回查询的SELECT部分。CDbCommand
getText()返回返回将被执行的SQL语句。CDbCommand
getUnion()返回查询的 UNION 部分。CDbCommand
getWhere()返回查询的WHERE部分。CDbCommand
group()设置查询的 GROUP BY 部分。CDbCommand
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
having()设置查询的 HAVING 部分。CDbCommand
insert()创建和执行一个SQL语句。CDbCommand
join()附加一个INNER JOIN部分到此查询。CDbCommand
leftJoin()附加一个LEFT OUTER JOIN部分到此查询。CDbCommand
limit()设置查询的 LIMIT 部分。CDbCommand
naturalJoin()附加一个NATURAL JOIN 部分到查询。CDbCommand
offset()设置查询的 OFFSET 部分。CDbCommand
order()设置此查询的ORDER BY部分。CDbCommand
prepare()准备要执行的SQL语句。CDbCommand
query()执行SQL语句并返回查询结果。CDbCommand
queryAll()执行SQL语句并返回所有行。CDbCommand
queryColumn()执行SQL语句并返回结果的第一列。CDbCommand
queryRow()执行此SQL语句并返回结果的第一行。CDbCommand
queryScalar()执行此SQL语句并返回第一行数据的第一列的值。CDbCommand
raiseEvent()发起一个事件。CComponent
renameColumn()绑定并执行一条SQL语句为重命名一个列。CDbCommand
renameTable()绑定并执行一条SQL语句为重命名一个数据表。CDbCommand
reset()清理命令,并准备建立一个新的查询。CDbCommand
rightJoin()附加一个RIGHT OUTER JOIN部分到此查询。CDbCommand
select()设置查询的SELECT部分。CDbCommand
selectDistinct()设置此查询的SELECT部分,带有DISTINCT标志开关。CDbCommand
setDistinct()设置一个值指示是否SELECT DISTINCT应该被使用。CDbCommand
setFetchMode()设置此语句的默认读取模式。CDbCommand
setFrom()设置查询的FROM部分。CDbCommand
setGroup()设置查询的 GROUP BY 部分。CDbCommand
setHaving()设置查询的 HAVING 部分。CDbCommand
setJoin()设置查询的join部分。CDbCommand
setLimit()设置查询的 LIMIT 部分。CDbCommand
setOffset()设置查询的 OFFSET 部分。CDbCommand
setOrder()设置查询的 ORDER BY 部分。CDbCommand
setSelect()设置的SELECT部分。CDbCommand
setText()指定要执行的SQL语句。CDbCommand
setUnion()设置查询的 UNION 部分。CDbCommand
setWhere()设置查询的 WHERE 部分。CDbCommand
truncateTable()绑定并执行一条SQL语句为清空一个数据表。CDbCommand
union()附加一个UNION操作符到一个SQL语句。CDbCommand
update()创建并执行一条UPDATE SQL语句。CDbCommand
where()设置查询的WHERE。CDbCommand

属性详细

connection 属性 只读 public CDbConnection getConnection()

和这个命令相关的连接。

distinct 属性 (可用自 v1.1.6) public boolean getDistinct()
public void setDistinct(boolean $value)

返回一个值指示是否SELECT DISTINCT应该被使用。

from 属性 (可用自 v1.1.6) public string getFrom()
public void setFrom(mixed $value)

返回查询的 FROM 部分。

group 属性 (可用自 v1.1.6) public string getGroup()
public void setGroup(mixed $value)

返回查询中的 GROUP BY 部分。

having 属性 (可用自 v1.1.6) public string getHaving()
public void setHaving(mixed $value)

返回查询的HAVING部分。

join 属性 (可用自 v1.1.6) public mixed getJoin()
public void setJoin(mixed $value)

返回查询的join部分。

limit 属性 (可用自 v1.1.6) public string getLimit()
public void setLimit(integer $value)

返回查询的 LIMIT 部分。

offset 属性 (可用自 v1.1.6) public string getOffset()
public void setOffset(integer $value)

返回查询的 OFFSET 部分。

order 属性 (可用自 v1.1.6) public string getOrder()
public void setOrder(mixed $value)

返回查询的 ORDER BY 部分。

params 属性 (可用自 v1.1.6) public array $params;

参数(name=>value)被绑定要查询的当前语句。

pdoStatement 属性 只读 public PDOStatement getPdoStatement()

这个命令底层的PDOStatement 如果尚未准备好语句,它可以是null。

select 属性 (可用自 v1.1.6) public string getSelect()
public void setSelect(mixed $value)

返回查询的SELECT部分。

text 属性 public string getText()
public CDbCommand setText(string $value)

返回将被执行的SQL语句。

union 属性 (可用自 v1.1.6) public mixed getUnion()
public void setUnion(mixed $value)

返回查询的 UNION 部分。

where 属性 (可用自 v1.1.6) public string getWhere()
public void setWhere(mixed $value)

返回查询的WHERE部分。

方法详细

__construct() 方法
public void __construct(CDbConnection $connection, mixed $query=NULL)
$connectionCDbConnection数据库连接
$querymixed要执行的数据库查询。这个可以是代表SQL语句的一个字符串或一个数组, 它的键名-键值对将被用作设置, 创建相应对象命令的属性。

例如,你能传递 ‘SELECT * FROM tbl_user’array(‘select’=>‘*’,‘from’=>‘tbl_user’)。 他们最终的查询结果等同。

当传递查询为一个数组时,通常要设置下面的属性: select,distinct,from,where,join, group,having,order,limit,offset和union。 请参考这些属性每个设置的更多细节。 关于验证属性。这个功能可用自版本 1.1.6。

自 1.1.7 通过设置FetchMode使用一个特定的数据获取模式是可能的。参见http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php 为更多详细信息。
源码: framework/db/CDbCommand.php#101 (显示) publicfunction__construct(CDbConnection$connection,$query=null)
{
$this->_connection=$connection;
if(is_array($query))
{
foreach($queryas$name=>$value)
$this->$name=$value;
}
else
$this->setText($query);
}

构造函数。

__sleep() 方法
public array __sleep()
{return}array
源码: framework/db/CDbCommand.php#117 (显示) publicfunction__sleep()
{
$this->_statement=null;
returnarray_keys(get_object_vars($this));
}

当序列化时语句声明为null。

addColumn() 方法 (可用自 v1.1.6)
public integer addColumn(string $table, string $column, string $type)
$tablestring新列将被添加进去的表。此表名将被此方法恰当的引用。
$columnstring新列的名称。此名称将被该方法恰当的引用。
$typestring列类型,getColumnType方法将被触发转换抽象的列类型(若有)为实际类型。 任何不能识别的抽象类型将保持在生成的SQL中。 例如,'string'将被转换成'varchar(255)', 同时 'string not null' 将变成 'varchar(255) not null'.
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1302 (显示) publicfunctionaddColumn($table,$column,$type)
{
return$this->setText($this->getConnection()->getSchema()->addColumn($table,$column,$type))->execute();
}

绑定并执行一条SQL语句为增加一个新的数据列。

addForeignKey() 方法 (可用自 v1.1.6)
public integer addForeignKey(string $name, string $table, string $columns, string $refTable, string $refColumns, string $delete=NULL, string $update=NULL)
$namestring外键结束的名称。
$tablestring外键结束将被添加进去的表。
$columnsstring约束将被添加到的表名。如果有多列,用逗号分开它们。
$refTablestring外键引用的表。
$refColumnsstring外键引用的列的名称。如果有多列,用逗号分开它们。
$deletestringON DELETE 选项。 大部分 DBMS 支持这个选项: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
$updatestringON UPDATE 选项。 大部分 DBMS 支持这个选项: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1360 (显示) publicfunctionaddForeignKey($name,$table,$columns,$refTable,$refColumns,$delete=null,$update=null)
{
return$this->setText($this->getConnection()->getSchema()->addForeignKey($name,$table,$columns,$refTable,$refColumns,$delete,$update))->execute();
}

为已经存在的数据表创建一条外键约束的SQL语句。 这个方法将恰当的引用此列和列名。

alterColumn() 方法 (可用自 v1.1.6)
public integer alterColumn(string $table, string $column, string $type)
$tablestring列将被改变的表。表名将被这个方法恰当的引用。
$columnstring要更改的列的名称。此名称将被该方法恰当的引用。
$typestring新列的类型。getColumnType 方法将被触发转换抽象列类型(若有) 为实际类型。任何无法识别的抽象类型将被保持在已经生成的SQL中。 例如,'string' 将被转换成 'varchar(255)', 同时 'string not null' 将变成 'varchar(255) not null'。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1342 (显示) publicfunctionalterColumn($table,$column,$type)
{
return$this->setText($this->getConnection()->getSchema()->alterColumn($table,$column,$type))->execute();
}

绑定并执行一条SQL语句为改变一个列的定义。

bindParam() 方法
public CDbCommand bindParam(mixed $name, mixed &$value, integer $dataType=NULL, integer $length=NULL, mixed $driverOptions=NULL)
$namemixed参数标识符。 对于一个使用命名占位符的预准备语句,这将是一个表单名称参数:name。 对于一个使用问号占位符的预准备语句, 这将是一个从1开始的参数位置索引。
$valuemixed绑定到SQL语句参数的PHP变量。
$dataTypeinteger参数的SQL数据类型。如果为null,此类型由PHP值的类型决定。
$lengthinteger数据类型的长度
$driverOptionsmixed指定的驱动选项(自版本1.1.6可用)
{return}CDbCommand当前要执行的命令
源码: framework/db/CDbCommand.php#245 (显示) publicfunctionbindParam($name,&$value,$dataType=null,$length=null,$driverOptions=null)
{
$this->prepare();
if($dataType===null)
$this->_statement->bindParam($name,$value,$this->_connection->getPdoType(gettype($value)));
elseif($length===null)
$this->_statement->bindParam($name,$value,$dataType);
elseif($driverOptions===null)
$this->_statement->bindParam($name,$value,$dataType,$length);
else
$this->_statement->bindParam($name,$value,$dataType,$length,$driverOptions);
$this->_paramLog[$name]=&$value;
return$this;
}

绑定一个参数到要执行的语句。

参见

  • http://www.php.net/manual/en/function.PDOStatement-bindParam.php
bindValue() 方法
public CDbCommand bindValue(mixed $name, mixed $value, integer $dataType=NULL)
$namemixed参数标签符。 对于一个使用命名占位符的预准备语句, 这将是一个表单名称参数:name, 对于一个使用问号占位符的预准备语句, 这将是一个从1开始的参数位置索引。
$valuemixed绑定到此参数的值
$dataTypeinteger参数的SQL数据类型。如果为null,此类型由PHP值的类型决定。
{return}CDbCommand当前要执行的命令
源码: framework/db/CDbCommand.php#271 (显示) publicfunctionbindValue($name,$value,$dataType=null)
{
$this->prepare();
if($dataType===null)
$this->_statement->bindValue($name,$value,$this->_connection->getPdoType(gettype($value)));
else
$this->_statement->bindValue($name,$value,$dataType);
$this->_paramLog[$name]=$value;
return$this;
}

绑定一个值到参数。

参见

  • http://www.php.net/manual/en/function.PDOStatement-bindValue.php
bindValues() 方法 (可用自 v1.1.5)
public CDbCommand bindValues(array $values)
$valuesarray要被绑定的值。 这个必须由相应的数组键给定参数名,数组值给定参数值。 例如, array(‘:name’=>‘John’, ‘:age’=>25).
{return}CDbCommand返回当前将执行的命令
源码: framework/db/CDbCommand.php#292 (显示) publicfunctionbindValues($values)
{
$this->prepare();
foreach($valuesas$name=>$value)
{
$this->_statement->bindValue($name,$value,$this->_connection->getPdoType(gettype($value)));
$this->_paramLog[$name]=$value;
}
return$this;
}

将一个列表的值绑定到相应参数。 除了绑定多个值外,这个和 bindValue 是相似的。 注意每个值的SQL数据类型是由PHP类型决定。

buildQuery() 方法 (可用自 v1.1.6)
public string buildQuery(array $query)
$queryarray名称-值对查询规范。 下面的查询选项被支持。:select,distinct,from, where,join, group, having, order, limit,offset 和 union。
{return}string返回SQL语句
源码: framework/db/CDbCommand.php#542 (显示) publicfunctionbuildQuery($query)
{
$sql=isset($query['distinct'])&&$query['distinct']?'SELECTDISTINCT':'SELECT';
$sql.=''.(isset($query['select'])?$query['select']:'*');

if(isset($query['from']))
$sql.="nFROM".$query['from'];
else
thrownewCDbException(Yii::t('yii','TheDBquerymustcontainthe"from"portion.'));

if(isset($query['join']))
$sql.="n".(is_array($query['join'])?implode("n",$query['join']):$query['join']);

if(isset($query['where']))
$sql.="nWHERE".$query['where'];

if(isset($query['group']))
$sql.="nGROUPBY".$query['group'];

if(isset($query['having']))
$sql.="nHAVING".$query['having'];

if(isset($query['order']))
$sql.="nORDERBY".$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;
}

生成一个SQL SELECT语句从给定查询规范。

cancel() 方法
public void cancel()
源码: framework/db/CDbCommand.php#227 (显示) publicfunctioncancel()
{
$this->_statement=null;
}

取消SQL语句的执行。

createIndex() 方法 (可用自 v1.1.6)
public integer createIndex(string $name, string $table, string $column, boolean $unique=false)
$namestring索引的名称。此名称将被该方法恰当的引用。
$tablestring要为其创建新索引的表。表名将这个方法恰当的引用。
$columnstring被包含在索引中的列。如果有多列,请用逗号分开它们。 此列名将被该方法恰当的引用。
$uniqueboolean是否在创建索引时增加UNIQUE约束。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1387 (显示) publicfunctioncreateIndex($name,$table,$column,$unique=false)
{
return$this->setText($this->getConnection()->getSchema()->createIndex($name,$table,$column,$unique))->execute();
}

绑定并执行一条SQL语句为创建一个新索引。

createTable() 方法 (可用自 v1.1.6)
public integer createTable(string $table, array $columns, string $options=NULL)
$tablestring被创建的表的名称。此名称将被该方法恰当的引用。
$columnsarray新表中的列(name=>definition)。
$optionsstring额外的SQL片段,将附加到生成的SQL中。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1249 (显示) publicfunctioncreateTable($table,$columns,$options=null)
{
return$this->setText($this->getConnection()->getSchema()->createTable($table,$columns,$options))->execute();
}

绑定并执行一条SQL语句为创建一个数据表。

应将新表中的列指定为名称-定义对(例如'name'=>'string'), 这里 name 代表一个列名,它将被这个方法恰当的引用,而 definition 代表列的类型,它能包含一个抽象的DB类型。 getColumnType 方法将被触发转换任何抽象类型为一个实际类型。

如果仅指定了列的定义(例如 'PRIMARY KEY (name, type)'),它将被直接插入 生成的 SQL中。

crossJoin() 方法 (可用自 v1.1.6)
public CDbCommand crossJoin(string $table)
$tablestring要被连接的表。 表可包含schema前缀(例如 ‘public.tbl_user’)和/或 表别名 (例如 ‘tbl_user u’)。 这个方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式)。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#878 (显示) publicfunctioncrossJoin($table)
{
return$this->joinInternal('crossjoin',$table);
}

附加一个CROSS JOIN部分到此查询。 注意不是所有的DBMS都支持CROSS JOIN.

delete() 方法 (可用自 v1.1.6)
public integer delete(string $table, mixed $conditions='', array $params=array ( ))
$tablestring数据将要被删除的表。
$conditionsmixed将被放入WHERE 部分的条件。 请参考where关于怎样指定条件。
$paramsarray被绑定到查询的参数。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1224 (显示) publicfunctiondelete($table,$conditions='',$params=array())
{
$sql='DELETEFROM'.$this->_connection->quoteTableName($table);
if(($where=$this->processConditions($conditions))!='')
$sql.='WHERE'.$where;
return$this->setText($sql)->execute($params);
}

创建并执行一条 DELETE SQL 语句。

dropColumn() 方法 (可用自 v1.1.6)
public integer dropColumn(string $table, string $column)
$tablestring列将被删除的表,此名称将被该方法恰当的引用。
$columnstring将被删除的列的名称。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1314 (显示) publicfunctiondropColumn($table,$column)
{
return$this->setText($this->getConnection()->getSchema()->dropColumn($table,$column))->execute();
}

绑定并执行一条SQL语句为删除一个数据列。

dropForeignKey() 方法 (可用自 v1.1.6)
public integer dropForeignKey(string $name, string $table)
$namestring要被删除的外键约束名称。此名称将被该方法恰当的引用。
$tablestring要被删除的外键的表。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1372 (显示) publicfunctiondropForeignKey($name,$table)
{
return$this->setText($this->getConnection()->getSchema()->dropForeignKey($name,$table))->execute();
}

建立一条SQL语句为删除一个外键约束。

dropIndex() 方法 (可用自 v1.1.6)
public integer dropIndex(string $name, string $table)
$namestring要删除的索引名称。此名称将被该方法恰当的引用。
$tablestring索引要被删除的表。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1399 (显示) publicfunctiondropIndex($name,$table)
{
return$this->setText($this->getConnection()->getSchema()->dropIndex($name,$table))->execute();
}

绑定并执行一条SQL语句为删除一个索引。

dropTable() 方法 (可用自 v1.1.6)
public integer dropTable(string $table)
$tablestring要删除的表。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1272 (显示) publicfunctiondropTable($table)
{
return$this->setText($this->getConnection()->getSchema()->dropTable($table))->execute();
}

绑定并执行一条SQL语句为删除一个数据表

execute() 方法
public integer execute(array $params=array ( ))
$paramsarray为SQL执行的输入参数 (name=>value)。 对于bindParam 和 bindValue这是二选一的。 如果你有多个输入参数,用这种方式传递他们能提高性能。 注意,如果你用这种方式传递参数,你不能使用 bindParam或者bindValue绑定参数或值, 反之亦然,这样可以提高性能。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#315 (显示) publicfunctionexecute($params=array())
{
if($this->_connection->enableParamLogging&&($pars=array_merge($this->_paramLog,$params))!==array())
{
$p=array();
foreach($parsas$name=>$value)
$p[$name]=$name.'='.var_export($value,true);
$par='.Boundwith'.implode(',',$p);
}
else
$par='';
Yii::trace('ExecutingSQL:'.$this->getText().$par,'system.db.CDbCommand');
try
{
if($this->_connection->enableProfiling)
Yii::beginProfile('system.db.CDbCommand.execute('.$this->getText().')','system.db.CDbCommand.execute');

$this->prepare();
if($params===array())
$this->_statement->execute();
else
$this->_statement->execute($params);
$n=$this->_statement->rowCount();

if($this->_connection->enableProfiling)
Yii::endProfile('system.db.CDbCommand.execute('.$this->getText().')','system.db.CDbCommand.execute');

return$n;
}
catch(Exception$e)
{
if($this->_connection->enableProfiling)
Yii::endProfile('system.db.CDbCommand.execute('.$this->getText().')','system.db.CDbCommand.execute');
$errorInfo=$einstanceofPDOException?$e->errorInfo:null;
$message=$e->getMessage();
Yii::log(Yii::t('yii','CDbCommand::execute()failed:{error}.TheSQLstatementexecutedwas:{sql}.',
array('{error}'=>$message,'{sql}'=>$this->getText().$par)),CLogger::LEVEL_ERROR,'system.db.CDbCommand');
if(YII_DEBUG)
$message.='.TheSQLstatementexecutedwas:'.$this->getText().$par;
thrownewCDbException(Yii::t('yii','CDbCommandfailedtoexecutetheSQLstatement:{error}',
array('{error}'=>$message)),(int)$e->getCode(),$errorInfo);
}
}

执行此 SQL 语句。 此方法意味着只执行 non-query SQL 语句。 将不返回结果集。

from() 方法 (可用自 v1.1.6)
public CDbCommand from(mixed $tables)
$tablesmixedfrom选择的表(可能是多个)。这个可能是一个字符串(例如 'tbl_user') 或一个数组 (例如 array('tbl_user', 'tbl_profile')) 指定一个或多个表名。 表名能包含schema前缀 (例如 'public.tbl_user') 和/或 表别名 (例如 'tbl_user u'). 此方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式).
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#682 (显示) publicfunctionfrom($tables)
{
if(is_string($tables)&&strpos($tables,'(')!==false)
$this->_query['from']=$tables;
else
{
if(!is_array($tables))
$tables=preg_split('/s*,s*/',trim($tables),-1,PREG_SPLIT_NO_EMPTY);
foreach($tablesas$i=>$table)
{
if(strpos($table,'(')===false)
{
if(preg_match('/^(.*?)(?i:s+ass+|s+)(.*)$/',$table,$matches))//withalias
$tables[$i]=$this->_connection->quoteTableName($matches[1]).''.$this->_connection->quoteTableName($matches[2]);
else
$tables[$i]=$this->_connection->quoteTableName($table);
}
}
$this->_query['from']=implode(',',$tables);
}
return$this;
}

设置查询的FROM部分。

getConnection() 方法
public CDbConnection getConnection()
{return}CDbConnection和这个命令相关的连接。
源码: framework/db/CDbCommand.php#184 (显示) publicfunctiongetConnection()
{
return$this->_connection;
}
getDistinct() 方法 (可用自 v1.1.6)
public boolean getDistinct()
{return}boolean返回一个值指示是否SELECT DISTINCT应该被使用。
源码: framework/db/CDbCommand.php#657 (显示) publicfunctiongetDistinct()
{
returnisset($this->_query['distinct'])?$this->_query['distinct']:false;
}

返回一个值指示是否SELECT DISTINCT应该被使用。

getFrom() 方法 (可用自 v1.1.6)
public string getFrom()
{return}string查询的FROM部分(没有 'FROM' )。
源码: framework/db/CDbCommand.php#710 (显示) publicfunctiongetFrom()
{
returnisset($this->_query['from'])?$this->_query['from']:'';
}

返回查询的 FROM 部分。

getGroup() 方法 (可用自 v1.1.6)
public string getGroup()
{return}string返回查询中的 GROUP BY 部分 (没有 ‘GROUP BY’ )。
源码: framework/db/CDbCommand.php#932 (显示) publicfunctiongetGroup()
{
returnisset($this->_query['group'])?$this->_query['group']:'';
}

返回查询中的 GROUP BY 部分。

getHaving() 方法 (可用自 v1.1.6)
public string getHaving()
{return}string返回查询的HAVING部分(没有 'HAVING' ) 。
源码: framework/db/CDbCommand.php#969 (显示) publicfunctiongetHaving()
{
returnisset($this->_query['having'])?$this->_query['having']:'';
}

返回查询的HAVING部分。

getJoin() 方法 (可用自 v1.1.6)
public mixed getJoin()
{return}mixed查询的join。 这个可以是一个数组表示多个join片断,或一个字符串表示单个join片断。 每一个join片断将包含恰当的join操作符(例如 LEFT JOIN)。
源码: framework/db/CDbCommand.php#817 (显示) publicfunctiongetJoin()
{
returnisset($this->_query['join'])?$this->_query['join']:'';
}

返回查询的join部分。

getLimit() 方法 (可用自 v1.1.6)
public string getLimit()
{return}string返回查询的 LIMIT 部分(没有 'LIMIT')。
源码: framework/db/CDbCommand.php#1060 (显示) publicfunctiongetLimit()
{
returnisset($this->_query['limit'])?$this->_query['limit']:-1;
}

返回查询的 LIMIT 部分。

getOffset() 方法 (可用自 v1.1.6)
public string getOffset()
{return}string查询的OFFSET部分(没有'OFFSET' )。
源码: framework/db/CDbCommand.php#1093 (显示) publicfunctiongetOffset()
{
returnisset($this->_query['offset'])?$this->_query['offset']:-1;
}

返回查询的 OFFSET 部分。

getOrder() 方法 (可用自 v1.1.6)
public string getOrder()
{return}string返回查询中的ORDER BY部分(没有 'ORDER BY' )。
源码: framework/db/CDbCommand.php#1024 (显示) publicfunctiongetOrder()
{
returnisset($this->_query['order'])?$this->_query['order']:'';
}

返回查询的 ORDER BY 部分。

getPdoStatement() 方法
public PDOStatement getPdoStatement()
{return}PDOStatement这个命令底层的PDOStatement 如果尚未准备好语句,它可以是null。
源码: framework/db/CDbCommand.php#193 (显示) publicfunctiongetPdoStatement()
{
return$this->_statement;
}
getSelect() 方法 (可用自 v1.1.6)
public string getSelect()
{return}string查询的SELECT部分(没有'SELECT')
源码: framework/db/CDbCommand.php#623 (显示) publicfunctiongetSelect()
{
returnisset($this->_query['select'])?$this->_query['select']:'';
}

返回查询的SELECT部分。

getText() 方法
public string getText()
{return}string返回将被执行的SQL语句。
源码: framework/db/CDbCommand.php#158 (显示) publicfunctiongetText()
{
if($this->_text==''&&!empty($this->_query))
$this->setText($this->buildQuery($this->_query));
return$this->_text;
}
getUnion() 方法 (可用自 v1.1.6)
public mixed getUnion()
{return}mixed查询的UNION部分(没有'UNION' )。 它可以是一个字符串或一个数组(表示多个union)部分。
源码: framework/db/CDbCommand.php#1131 (显示) publicfunctiongetUnion()
{
returnisset($this->_query['union'])?$this->_query['union']:'';
}

返回查询的 UNION 部分。

getWhere() 方法 (可用自 v1.1.6)
public string getWhere()
{return}string查询的WHERE部分 (没有 'WHERE' )。
源码: framework/db/CDbCommand.php#777 (显示) publicfunctiongetWhere()
{
returnisset($this->_query['where'])?$this->_query['where']:'';
}

返回查询的WHERE部分。

group() 方法 (可用自 v1.1.6)
public CDbCommand group(mixed $columns)
$columnsmixed将被分组的列。 被指定的列可能是一个字符串(例如 "id, name")或者一个数组(例如 array('id', 'name')). 这个方法将自动的引用此列名,除非一个列包含一些圆括号, (这意味着此列包含一个DB表达式)。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#907 (显示) publicfunctiongroup($columns)
{
if(is_string($columns)&&strpos($columns,'(')!==false)
$this->_query['group']=$columns;
else
{
if(!is_array($columns))
$columns=preg_split('/s*,s*/',trim($columns),-1,PREG_SPLIT_NO_EMPTY);
foreach($columnsas$i=>$column)
{
if(is_object($column))
$columns[$i]=(string)$column;
elseif(strpos($column,'(')===false)
$columns[$i]=$this->_connection->quoteColumnName($column);
}
$this->_query['group']=implode(',',$columns);
}
return$this;
}

设置查询的 GROUP BY 部分。

having() 方法 (可用自 v1.1.6)
public CDbCommand having(mixed $conditions, array $params=array ( ))
$conditionsmixed放在HAVING后的条件。 请参考where关于怎样指定条件。
$paramsarray绑定到此查询的参数 (name=>value)
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#956 (显示) publicfunctionhaving($conditions,$params=array())
{
$this->_query['having']=$this->processConditions($conditions);
foreach($paramsas$name=>$value)
$this->params[$name]=$value;
return$this;
}

设置查询的 HAVING 部分。

insert() 方法 (可用自 v1.1.6)
public integer insert(string $table, array $columns)
$tablestring将被插入的表。
$columnsarray要插入表的列数据(name=>value)。
{return}integer返回此执行影响的行数。
源码: framework/db/CDbCommand.php#1155 (显示) publicfunctioninsert($table,$columns)
{
$params=array();
$names=array();
$placeholders=array();
foreach($columnsas$name=>$value)
{
$names[]=$this->_connection->quoteColumnName($name);
if($valueinstanceofCDbExpression)
{
$placeholders[]=$value->expression;
foreach($value->paramsas$n=>$v)
$params[$n]=$v;
}
else
{
$placeholders[]=':'.$name;
$params[':'.$name]=$value;
}
}
$sql='INSERTINTO'.$this->_connection->quoteTableName($table)
.'('.implode(',',$names).')VALUES('
.implode(',',$placeholders).')';
return$this->setText($sql)->execute($params);
}

创建和执行一个SQL语句。 该方法将恰当的转义列名,并绑定要插入的值。

join() 方法 (可用自 v1.1.6)
public CDbCommand join(string $table, mixed $conditions, array $params=array ( ))
$tablestring要被连接的表。 表可包含schema前缀(例如 'public.tbl_user')和/或 表别名 (例如 'tbl_user u')。 这个方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式)。
$conditionsmixed出现在ON部分的join条件。 请参考where关于怎样指定条件。
$paramsarray绑定到此查询的参数 (name=>value)
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#805 (显示) publicfunctionjoin($table,$conditions,$params=array())
{
return$this->joinInternal('join',$table,$conditions,$params);
}

附加一个INNER JOIN部分到此查询。

leftJoin() 方法 (可用自 v1.1.6)
public CDbCommand leftJoin(string $table, mixed $conditions, array $params=array ( ))
$tablestring要被连接的表。 表可包含schema前缀(例如 ‘public.tbl_user’)和/或 表别名 (例如 ‘tbl_user u’)。 这个方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式)。
$conditionsmixed出现在ON部分的join条件。 请参考where关于怎样指定条件。
$paramsarray绑定到此查询的参数 (name=>value)
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#846 (显示) publicfunctionleftJoin($table,$conditions,$params=array())
{
return$this->joinInternal('leftjoin',$table,$conditions,$params);
}

附加一个LEFT OUTER JOIN部分到此查询。

limit() 方法 (可用自 v1.1.6)
public CDbCommand limit(integer $limit, integer $offset=NULL)
$limitintegerlimit值
$offsetintegeroffset值
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#1047 (显示) publicfunctionlimit($limit,$offset=null)
{
$this->_query['limit']=(int)$limit;
if($offset!==null)
$this->offset($offset);
return$this;
}

设置查询的 LIMIT 部分。

naturalJoin() 方法 (可用自 v1.1.6)
public CDbCommand naturalJoin(string $table)
$tablestring要被连接的表。 表可包含schema前缀(例如 ‘public.tbl_user’)和/或 表别名 (例如 ‘tbl_user u’)。 这个方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式)。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#893 (显示) publicfunctionnaturalJoin($table)
{
return$this->joinInternal('naturaljoin',$table);
}

附加一个NATURAL JOIN 部分到查询。 注意不是所有的DBMS都支持NATURAL JOIN.

offset() 方法 (可用自 v1.1.6)
public CDbCommand offset(integer $offset)
$offsetintegeroffset值
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#1082 (显示) publicfunctionoffset($offset)
{
$this->_query['offset']=(int)$offset;
return$this;
}

设置查询的 OFFSET 部分。

order() 方法 (可用自 v1.1.6)
public CDbCommand order(mixed $columns)
$columnsmixed要被排序的列(和方向)。 指定的列或者是一个字符串(例如 "id ASC, name DESC")或者一个数组(例如 array('id ASC', 'name DESC'))。 这个方法将自动的引用此列名,除非一个列包含一些圆括号, (这意味着此列包含一个DB表达式)。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#994 (显示) publicfunctionorder($columns)
{
if(is_string($columns)&&strpos($columns,'(')!==false)
$this->_query['order']=$columns;
else
{
if(!is_array($columns))
$columns=preg_split('/s*,s*/',trim($columns),-1,PREG_SPLIT_NO_EMPTY);
foreach($columnsas$i=>$column)
{
if(is_object($column))
$columns[$i]=(string)$column;
elseif(strpos($column,'(')===false)
{
if(preg_match('/^(.*?)s+(asc|desc)$/i',$column,$matches))
$columns[$i]=$this->_connection->quoteColumnName($matches[1]).''.strtoupper($matches[2]);
else
$columns[$i]=$this->_connection->quoteColumnName($column);
}
}
$this->_query['order']=implode(',',$columns);
}
return$this;
}

设置此查询的ORDER BY部分。

prepare() 方法
public void prepare()
源码: framework/db/CDbCommand.php#205 (显示) publicfunctionprepare()
{
if($this->_statement==null)
{
try
{
$this->_statement=$this->getConnection()->getPdoInstance()->prepare($this->getText());
$this->_paramLog=array();
}
catch(Exception$e)
{
Yii::log('ErrorinpreparingSQL:'.$this->getText(),CLogger::LEVEL_ERROR,'system.db.CDbCommand');
$errorInfo=$einstanceofPDOException?$e->errorInfo:null;
thrownewCDbException(Yii::t('yii','CDbCommandfailedtopreparetheSQLstatement:{error}',
array('{error}'=>$e->getMessage())),(int)$e->getCode(),$errorInfo);
}
}
}

准备要执行的SQL语句。 对于要多次执行的复杂的SQL语句, 这个可以提高性能。 对于绑定了参数的SQL语句, 这个方法会被自动触发。

query() 方法
public CDbDataReader query(array $params=array ( ))
$paramsarray为SQL执行的输入基数 (name=>value)。 这是bindParam 和 bindValue二选一的。 如果你有多个输入参数,用这种方式传递它们能提高性能。 注意如果你用这种方式传递参数,你不能使用 bindParam或 bindValue绑定参数或值,反之亦然。 绑定方法和输入参数,这样可以提高性能。
{return}CDbDataReader获取查询结果的读取器对象
源码: framework/db/CDbCommand.php#370 (显示) publicfunctionquery($params=array())
{
return$this->queryInternal('',0,$params);
}

执行SQL语句并返回查询结果。 此方法执行返回结果集的SQL查询。

queryAll() 方法
public array queryAll(boolean $fetchAssociative=true, array $params=array ( ))
$fetchAssociativeboolean是否每一行应该被作为一个关联数组返回, 关联数组的列名为键或列索引作为键(从0开始)。
$paramsarraySQL执行的输入参数(name=>value)。这是 bindParam 和 bindValue二选一的。如果你有多个输入参数, 用这种方式传递它们能提高性能。注意如果你用这种方式传递参数, 你不能使用 bindParam或 bindValue绑定参数或值,反之亦然。 绑定方法和输入参数,这样可以提高性能。
{return}array返回查询结果中的所有行。每个数组元素是一个数组,表示一行。 如果查询结果为空,返回空数组。
源码: framework/db/CDbCommand.php#388 (显示) publicfunctionqueryAll($fetchAssociative=true,$params=array())
{
return$this->queryInternal('fetchAll',$fetchAssociative?$this->_fetchMode:PDO::FETCH_NUM,$params);
}

执行SQL语句并返回所有行。

queryColumn() 方法
public array queryColumn(array $params=array ( ))
$paramsarraySQL执行的输入参数(name=>value)。这是 bindParam和bindValue二选一的。 如果你有多个输入参数, 用这种方式传递它们能提高性能。注意如果你用这种方式传递参数, 你不能使用 bindParam或bindValue绑定参数或值,反之亦然。 绑定方法和输入参数,这样可以提高性能。
{return}array返回查询结果的第一列。 如果没有结果为空数组。
源码: framework/db/CDbCommand.php#444 (显示) publicfunctionqueryColumn($params=array())
{
return$this->queryInternal('fetchAll',PDO::FETCH_COLUMN,$params);
}

执行SQL语句并返回结果的第一列。 当只需要一个值的时候,这个方法比query更方便, 注意,返回的列将包含每一个结果行的第一个元素。

queryRow() 方法
public mixed queryRow(boolean $fetchAssociative=true, array $params=array ( ))
$fetchAssociativeboolean是否每一行应该被作为一个关联数组返回, 关联数组的列名为键或列索引作为键(从0开始)。
$paramsarraySQL执行的输入参数 (name=>value)。 这是 bindParam 和bindValue二选一的。 如果你有多个输入参数,用这种方式传递它们能提高性能。注意如果你用这种方式传递参数, 你不能使用 bindParam或 bindValue绑定参数或值,反之亦然。 绑定方法和输入参数,这样可以提高性能。
{return}mixedthe first row (in terms of an array) of the query result, false if no result.
源码: framework/db/CDbCommand.php#406 (显示) publicfunctionqueryRow($fetchAssociative=true,$params=array())
{
return$this->queryInternal('fetch',$fetchAssociative?$this->_fetchMode:PDO::FETCH_NUM,$params);
}

执行此SQL语句并返回结果的第一行。 当只需要第一行数据时,这个方法比query更方便。

queryScalar() 方法
public mixed queryScalar(array $params=array ( ))
$paramsarraySQL执行的输入参数 (name=>value)。这是 bindParam 的bindValue二选一的。 如果你有多个输入参数, 用这种方式传递它们能提高性能。注意如果你用这种方式传递参数, 你不能使用 bindParam或 bindValue绑定参数或值,反之亦然。 绑定方法和输入参数,这样可以提高性能。
{return}mixed返回查询结果的第一行数据的第一列的值。如果没有值返回False。
源码: framework/db/CDbCommand.php#423 (显示) publicfunctionqueryScalar($params=array())
{
$result=$this->queryInternal('fetchColumn',0,$params);
if(is_resource($result)&&get_resource_type($result)==='stream')
returnstream_get_contents($result);
else
return$result;
}

执行此SQL语句并返回第一行数据的第一列的值。 当只需要一个值的时候,这个方法比query更方便, (例如 获取记录的数量)。

renameColumn() 方法 (可用自 v1.1.6)
public integer renameColumn(string $table, string $name, string $newName)
$tablestring列将被重命名的表。此名称将被该方法恰当的引用。
$namestring此列的旧表名。此名称将被该方法恰当的引用。
$newNamestring此列的新名字。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1327 (显示) publicfunctionrenameColumn($table,$name,$newName)
{
return$this->setText($this->getConnection()->getSchema()->renameColumn($table,$name,$newName))->execute();
}

绑定并执行一条SQL语句为重命名一个列。

renameTable() 方法 (可用自 v1.1.6)
public integer renameTable(string $table, string $newName)
$tablestring要被重命名的表。此名称将这个方法恰当的引用。
$newNamestring新的表名。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1261 (显示) publicfunctionrenameTable($table,$newName)
{
return$this->setText($this->getConnection()->getSchema()->renameTable($table,$newName))->execute();
}

绑定并执行一条SQL语句为重命名一个数据表。

reset() 方法 (可用自 v1.1.6)
public CDbCommand reset()
{return}CDbCommand返回这个命令实例
源码: framework/db/CDbCommand.php#145 (显示) publicfunctionreset()
{
$this->_text=null;
$this->_query=null;
$this->_statement=null;
$this->_paramLog=array();
$this->params=array();
return$this;
}

清理命令,并准备建立一个新的查询。 这个方法主要用作当一个命令对象被多次重用时, 建立不同的查询。 调用此方法将清除所有命令对象的内部状态。

rightJoin() 方法 (可用自 v1.1.6)
public CDbCommand rightJoin(string $table, mixed $conditions, array $params=array ( ))
$tablestring要被连接的表。 表可包含schema前缀(例如 ‘public.tbl_user’)和/或 表别名 (例如 ‘tbl_user u’)。 这个方法将自动的引用此表名,除非它包含一些圆括号 (这意味着此表被给定作为一个子查询或DB表达式).
$conditionsmixed出现在ON部分的join条件。 请参考where关于怎样指定条件。
$paramsarray绑定到此查询的参数 (name=>value)
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#863 (显示) publicfunctionrightJoin($table,$conditions,$params=array())
{
return$this->joinInternal('rightjoin',$table,$conditions,$params);
}

附加一个RIGHT OUTER JOIN部分到此查询。

select() 方法 (可用自 v1.1.6)
public CDbCommand select(mixed $columns='*', string $option='')
$columnsmixed被选择的列。 默认值为 '*',意思是所有的列。 列能被一个字符串指定(例如 "id, name")或一个数组(例如 array('id', 'name')). 列能包含表前缀(例如 "tbl_user.id") 和/或 列别名(例如 "tbl_user.id AS user_id")。 这个方法将自动的引用此列名,除非一个列包含一些圆括号 (这意味着此列包含一个DB表达式).
$optionstring附加选择应该被附加到'SELECT'关键字。 例如,在MySQL,选项'SQL_CALC_FOUND_ROWS'能被使用。这个参数自版本1.1.8开始支持。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#590 (显示) publicfunctionselect($columns='*',$option='')
{
if(is_string($columns)&&strpos($columns,'(')!==false)
$this->_query['select']=$columns;
else
{
if(!is_array($columns))
$columns=preg_split('/s*,s*/',trim($columns),-1,PREG_SPLIT_NO_EMPTY);

foreach($columnsas$i=>$column)
{
if(is_object($column))
$columns[$i]=(string)$column;
elseif(strpos($column,'(')===false)
{
if(preg_match('/^(.*?)(?i:s+ass+|s+)(.*)$/',$column,$matches))
$columns[$i]=$this->_connection->quoteColumnName($matches[1]).'AS'.$this->_connection->quoteColumnName($matches[2]);
else
$columns[$i]=$this->_connection->quoteColumnName($column);
}
}
$this->_query['select']=implode(',',$columns);
}
if($option!='')
$this->_query['select']=$option.''.$this->_query['select'];
return$this;
}

设置查询的SELECT部分。

selectDistinct() 方法 (可用自 v1.1.6)
public CDbCommand selectDistinct(mixed $columns='*')
$columnsmixed被选择的列,参见select为更多信息。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#646 (显示) publicfunctionselectDistinct($columns='*')
{
$this->_query['distinct']=true;
return$this->select($columns);
}

设置此查询的SELECT部分,带有DISTINCT标志开关。 除了DISTINCT标志开关,这个和select是一样的。

setDistinct() 方法 (可用自 v1.1.6)
public void setDistinct(boolean $value)
$valueboolean一个值指示是否SELECT应该被使用。
源码: framework/db/CDbCommand.php#667 (显示) publicfunctionsetDistinct($value)
{
$this->_query['distinct']=$value;
}

设置一个值指示是否SELECT DISTINCT应该被使用。

setFetchMode() 方法 (可用自 v1.1.7)
public CDbCommand setFetchMode(mixed $mode)
$modemixed读取模式
{return}CDbCommand
源码: framework/db/CDbCommand.php#130 (显示) publicfunctionsetFetchMode($mode)
{
$params=func_get_args();
$this->_fetchMode=$params;
return$this;
}

设置此语句的默认读取模式。

参见

  • http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
setFrom() 方法 (可用自 v1.1.6)
public void setFrom(mixed $value)
$valuemixed被选择的表。 关于怎样指定这个参数请参考from()更多细节。
源码: framework/db/CDbCommand.php#721 (显示) publicfunctionsetFrom($value)
{
$this->from($value);
}

设置查询的FROM部分。

setGroup() 方法 (可用自 v1.1.6)
public void setGroup(mixed $value)
$valuemixedGROUP BY 部分。 关于怎样指定这个参数请参考 group() 关于更多细节。
源码: framework/db/CDbCommand.php#943 (显示) publicfunctionsetGroup($value)
{
$this->group($value);
}

设置查询的 GROUP BY 部分。

setHaving() 方法 (可用自 v1.1.6)
public void setHaving(mixed $value)
$valuemixedHAVING 部分。 关于怎样指定这个参数请参考having()更多细节。
源码: framework/db/CDbCommand.php#980 (显示) publicfunctionsetHaving($value)
{
$this->having($value);
}

设置查询的 HAVING 部分。

setJoin() 方法 (可用自 v1.1.6)
public void setJoin(mixed $value)
$valuemixed查询的join部分。 这个可能是一个字符串或一个数组,代表查询中多个join部分。 每一部分必须包含恰当的join制作符(例如 'LEFT JOIN tbl_profile ON tbl_user.id=tbl_profile.id')
源码: framework/db/CDbCommand.php#829 (显示) publicfunctionsetJoin($value)
{
$this->_query['join']=$value;
}

设置查询的join部分。

setLimit() 方法 (可用自 v1.1.6)
public void setLimit(integer $value)
$valueintegerLIMIT 部分。 关于怎样指定这个参数请参考 limit() 为更多细节。
源码: framework/db/CDbCommand.php#1071 (显示) publicfunctionsetLimit($value)
{
$this->limit($value);
}

设置查询的 LIMIT 部分。

setOffset() 方法 (可用自 v1.1.6)
public void setOffset(integer $value)
$valueintegerOFFSET 部分。 关于怎样指定这个参数请参考 offset() 为更多细节。
源码: framework/db/CDbCommand.php#1104 (显示) publicfunctionsetOffset($value)
{
$this->offset($value);
}

设置查询的 OFFSET 部分。

setOrder() 方法 (可用自 v1.1.6)
public void setOrder(mixed $value)
$valuemixedORDER BY 部分。 关于怎样指定这个参数请参考 order() 为更多细节。
源码: framework/db/CDbCommand.php#1035 (显示) publicfunctionsetOrder($value)
{
$this->order($value);
}

设置查询的 ORDER BY 部分。

setSelect() 方法 (可用自 v1.1.6)
public void setSelect(mixed $value)
$valuemixed被选择的数据。 请参考select()怎样指定这个参数的更多细节。
源码: framework/db/CDbCommand.php#634 (显示) publicfunctionsetSelect($value)
{
$this->select($value);
}

设置的SELECT部分。

setText() 方法
public CDbCommand setText(string $value)
$valuestring将被执行的SQL语句
{return}CDbCommand返回这个命令实例
源码: framework/db/CDbCommand.php#171 (显示) publicfunctionsetText($value)
{
if($this->_connection->tablePrefix!==null&&$value!='')
$this->_text=preg_replace('/{{(.*?)}}/',$this->_connection->tablePrefix.'1',$value);
else
$this->_text=$value;
$this->cancel();
return$this;
}

指定要执行的SQL语句。 之前任何查询都会终止或者取消。

setUnion() 方法 (可用自 v1.1.6)
public void setUnion(mixed $value)
$valuemixedUNION 部分。 这个可以是一个字符串或一个数组表示多个SQL语句被连接在一起。
源码: framework/db/CDbCommand.php#1142 (显示) publicfunctionsetUnion($value)
{
$this->_query['union']=$value;
}

设置查询的 UNION 部分。

setWhere() 方法 (可用自 v1.1.6)
public void setWhere(mixed $value)
$valuemixedwhere 部分。 关于怎样指定这个参数请参考where()更多细节。
源码: framework/db/CDbCommand.php#788 (显示) publicfunctionsetWhere($value)
{
$this->where($value);
}

设置查询的 WHERE 部分。

truncateTable() 方法 (可用自 v1.1.6)
public integer truncateTable(string $table)
$tablestring要被清空的表。此名称将被该方法恰当的引用。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1283 (显示) publicfunctiontruncateTable($table)
{
$schema=$this->getConnection()->getSchema();
$n=$this->setText($schema->truncateTable($table))->execute();
if(strncasecmp($this->getConnection()->getDriverName(),'sqlite',6)===0)
$schema->resetSequence($schema->getTable($table));
return$n;
}

绑定并执行一条SQL语句为清空一个数据表。

union() 方法 (可用自 v1.1.6)
public CDbCommand union(string $sql)
$sqlstring使用UNION要附加的SQL语句。
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#1115 (显示) publicfunctionunion($sql)
{
if(isset($this->_query['union'])&&is_string($this->_query['union']))
$this->_query['union']=array($this->_query['union']);

$this->_query['union'][]=$sql;

return$this;
}

附加一个UNION操作符到一个SQL语句。

update() 方法 (可用自 v1.1.6)
public integer update(string $table, array $columns, mixed $conditions='', array $params=array ( ))
$tablestring要更新的表。
$columnsarray要更新的列数据 (name=>value) 。
$conditionsmixed放入 WHERE 部分的条件。 请参考 where 怎样指定条件。
$paramsarray要绑定到此查询的参数。
{return}integer返回此操作影响的行数。
源码: framework/db/CDbCommand.php#1192 (显示) publicfunctionupdate($table,$columns,$conditions='',$params=array())
{
$lines=array();
foreach($columnsas$name=>$value)
{
if($valueinstanceofCDbExpression)
{
$lines[]=$this->_connection->quoteColumnName($name).'='.$value->expression;
foreach($value->paramsas$n=>$v)
$params[$n]=$v;
}
else
{
$lines[]=$this->_connection->quoteColumnName($name).'=:'.$name;
$params[':'.$name]=$value;
}
}
$sql='UPDATE'.$this->_connection->quoteTableName($table).'SET'.implode(',',$lines);
if(($where=$this->processConditions($conditions))!='')
$sql.='WHERE'.$where;
return$this->setText($sql)->execute($params);
}

创建并执行一条UPDATE SQL语句。 该方法将恰当的转义列名,并绑定要更新的值。

where() 方法 (可用自 v1.1.6)
public CDbCommand where(mixed $conditions, array $params=array ( ))
$conditionsmixed放在WHERE部分的条件。
$paramsarray绑定到此查询的参数 (name=>value)
{return}CDbCommand返回此命令对象本身
源码: framework/db/CDbCommand.php#764 (显示) publicfunctionwhere($conditions,$params=array())
{
$this->_query['where']=$this->processConditions($conditions);
foreach($paramsas$name=>$value)
$this->params[$name]=$value;
return$this;
}

设置查询的WHERE。

这个方法要求一个 $conditions 参数和一个 $params 参数, 指定值绑定到查询。

$conditions 参数可以是一个字符串(例如 'id=1')或一个数组。 如果是后者,它必须是这种格式 array(operator, operand1, operand2, ...), 操作符可以是下面当中的一个,可能的操作数依赖于相应的操作符 :

  • and: 操作数应该使用AND连接起来。例如, array('and', 'id=1', 'id=2') 将生成 'id=1 AND>
  • or: 和 and 操作符相似,除了操作数是使用OR连接起来。
  • in: 操作数1应该是一列或DB表达式,操作数2应该是一个数组, 表示相应的列的值或DB表达式应该在的范围。例如, array('in', 'id', array(1,2,3)) 将生成 'id IN (1,2,3)'. 这个方法将正确的引用列名和范围中的转义值。
  • not in: 和 in 相似,除了在生成条件时把IN替换成NOT IN.
  • like: 操作数1应该是一列或一个DB表达式,操作数2是一个字符串或一个数组 表示列或DB表达式应该like的值。 例如, array('like', 'name', '%tester%') 将生成 "name LIKE '%tester%'". 当值范围被给定为一个数组,多个LIKE谓语将被生成并使用AND连接起来。 例如, array('like', 'name', array('%test%', '%sample%')) 将生成 "name LIKE '%test%' AND name LIKE '%sample%'". 这个方法将正确的引用列名和范围中的转义值。
  • not like: 和 like相似,除了在生成条件时使用NOT LIKE替换LIKE。
  • or like: 和 like相似,除了OR被用作把LIKE谓语连接起来。
  • or not like: 和 not like相似,除了OR被用作把NOT LIKE谓语连接起来。