system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CDbDataReader

system.db
继承 class CDbDataReader » CComponent
实现 Iterator, Traversable, Countable
可用自 1.0
源码 framework/db/CDbDataReader.php
CDbDataReader represents a forward-only stream of rows from a query result set.

To read the current row of data, call read. The method readAll returns all the rows in a single array.

One can also retrieve the rows of data in CDbDataReader by using foreach:
foreach($reader as $row)
    // $row represents a row of data
Since CDbDataReader is a forward-only stream, you can only traverse it once.

It is possible to use a specific mode of data fetching by setting FetchMode. See http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php for more details.

公共属性

隐藏继承的属性

属性类型描述被定义在
columnCount integer Returns the number of columns in the result set. CDbDataReader
isClosed boolean whether the reader is closed or not. CDbDataReader
rowCount integer Returns the number of rows in the result set. CDbDataReader

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CDbDataReader
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
bindColumn() Binds a column to a PHP variable. CDbDataReader
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
close() Closes the reader. CDbDataReader
count() Returns the number of rows in the result set. CDbDataReader
current() Returns the current row. CDbDataReader
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getColumnCount() Returns the number of columns in the result set. CDbDataReader
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsClosed() whether the reader is closed or not. CDbDataReader
getRowCount() Returns the number of rows in the result set. CDbDataReader
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
key() Returns the index of the current row. CDbDataReader
next() Moves the internal pointer to the next row. CDbDataReader
nextResult() Advances the reader to the next result when reading the results of a batch of statements. CDbDataReader
raiseEvent() Raises an event. CComponent
read() Advances the reader to the next row in a result set. CDbDataReader
readAll() Reads the whole result set into an array. CDbDataReader
readColumn() Returns a single column from the next row of a result set. CDbDataReader
readObject() Returns an object populated with the next row of data. CDbDataReader
rewind() Resets the iterator to the initial state. CDbDataReader
setFetchMode() Set the default fetch mode for this statement CDbDataReader
valid() Returns whether there is a row of data at current position. CDbDataReader

属性详情

columnCount 属性 只读
public integer getColumnCount()

Returns the number of columns in the result set. Note, even there's no row in the reader, this still gives correct column number.

isClosed 属性 只读
public boolean getIsClosed()

whether the reader is closed or not.

rowCount 属性 只读
public integer getRowCount()

Returns the number of rows in the result set. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.

方法详情

__construct() 方法
public void __construct(CDbCommand $command)
$command CDbCommand the command generating the query result
源码: framework/db/CDbDataReader.php#48 (显示)
public function __construct(CDbCommand $command)
{
    
$this->_statement=$command->getPdoStatement();
    
$this->_statement->setFetchMode(PDO::FETCH_ASSOC);
}

Constructor.

bindColumn() 方法
public void bindColumn(mixed $column, mixed &$value, integer $dataType=NULL)
$column mixed Number of the column (1-indexed) or name of the column in the result set. If using the column name, be aware that the name should match the case of the column, as returned by the driver.
$value mixed Name of the PHP variable to which the column will be bound.
$dataType integer Data type of the parameter
源码: framework/db/CDbDataReader.php#65 (显示)
public function bindColumn($column, &$value$dataType=null)
{
    if(
$dataType===null)
        
$this->_statement->bindColumn($column,$value);
    else
        
$this->_statement->bindColumn($column,$value,$dataType);
}

Binds a column to a PHP variable. When rows of data are being fetched, the corresponding column value will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.

close() 方法
public void close()
源码: framework/db/CDbDataReader.php#142 (显示)
public function close()
{
    
$this->_statement->closeCursor();
    
$this->_closed=true;
}

Closes the reader. This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.

count() 方法
public integer count()
{return} integer number of rows contained in the result.
源码: framework/db/CDbDataReader.php#175 (显示)
public function count()
{
    return 
$this->getRowCount();
}

Returns the number of rows in the result set. This method is required by the Countable interface. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.

current() 方法
public mixed current()
{return} mixed the current row.
源码: framework/db/CDbDataReader.php#221 (显示)
public function current()
{
    return 
$this->_row;
}

Returns the current row. This method is required by the interface Iterator.

getColumnCount() 方法
public integer getColumnCount()
{return} integer the number of columns in the result set.
源码: framework/db/CDbDataReader.php#185 (显示)
public function getColumnCount()
{
    return 
$this->_statement->columnCount();
}

Returns the number of columns in the result set. Note, even there's no row in the reader, this still gives correct column number.

getIsClosed() 方法
public boolean getIsClosed()
{return} boolean whether the reader is closed or not.
源码: framework/db/CDbDataReader.php#152 (显示)
public function getIsClosed()
{
    return 
$this->_closed;
}

whether the reader is closed or not.

getRowCount() 方法
public integer getRowCount()
{return} integer number of rows contained in the result.
源码: framework/db/CDbDataReader.php#163 (显示)
public function getRowCount()
{
    return 
$this->_statement->rowCount();
}

Returns the number of rows in the result set. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.

key() 方法
public integer key()
{return} integer the index of the current row.
源码: framework/db/CDbDataReader.php#211 (显示)
public function key()
{
    return 
$this->_index;
}

Returns the index of the current row. This method is required by the interface Iterator.

next() 方法
public void next()
源码: framework/db/CDbDataReader.php#230 (显示)
public function next()
{
    
$this->_row=$this->_statement->fetch();
    
$this->_index++;
}

Moves the internal pointer to the next row. This method is required by the interface Iterator.

nextResult() 方法
public boolean nextResult()
{return} boolean Returns true on success or false on failure.
源码: framework/db/CDbDataReader.php#130 (显示)
public function nextResult()
{
    if((
$result=$this->_statement->nextRowset())!==false)
        
$this->_index=-1;
    return 
$result;
}

Advances the reader to the next result when reading the results of a batch of statements. This method is only useful when there are multiple result sets returned by the query. Not all DBMS support this feature.

read() 方法
public array|false read()
{return} array|false the current row, false if no more row available
源码: framework/db/CDbDataReader.php#88 (显示)
public function read()
{
    return 
$this->_statement->fetch();
}

Advances the reader to the next row in a result set.

readAll() 方法
public array readAll()
{return} array the result set (each array element represents a row of data). An empty array will be returned if the result contains no row.
源码: framework/db/CDbDataReader.php#119 (显示)
public function readAll()
{
    return 
$this->_statement->fetchAll();
}

Reads the whole result set into an array.

readColumn() 方法
public mixed|false readColumn(integer $columnIndex)
$columnIndex integer zero-based column index
{return} mixed|false the column of the current row, false if no more row available
源码: framework/db/CDbDataReader.php#98 (显示)
public function readColumn($columnIndex)
{
    return 
$this->_statement->fetchColumn($columnIndex);
}

Returns a single column from the next row of a result set.

readObject() 方法
public mixed|false readObject(string $className, array $fields)
$className string class name of the object to be created and populated
$fields array Elements of this array are passed to the constructor
{return} mixed|false the populated object, false if no more row of data available
源码: framework/db/CDbDataReader.php#109 (显示)
public function readObject($className,$fields)
{
    return 
$this->_statement->fetchObject($className,$fields);
}

Returns an object populated with the next row of data.

rewind() 方法
public void rewind()
源码: framework/db/CDbDataReader.php#195 (显示)
public function rewind()
{
    if(
$this->_index<0)
    {
        
$this->_row=$this->_statement->fetch();
        
$this->_index=0;
    }
    else
        throw new 
CDbException(Yii::t('yii','CDbDataReader cannot rewind. It is a forward-only reader.'));
}

Resets the iterator to the initial state. This method is required by the interface Iterator.

setFetchMode() 方法
public void setFetchMode(mixed $mode)
$mode mixed fetch mode
源码: framework/db/CDbDataReader.php#78 (显示)
public function setFetchMode($mode)
{
    
$params=func_get_args();
    
call_user_func_array(array($this->_statement,'setFetchMode'),$params);
}

Set the default fetch mode for this statement

valid() 方法
public boolean valid()
{return} boolean whether there is a row of data at current position.
源码: framework/db/CDbDataReader.php#241 (显示)
public function valid()
{
    return 
$this->_row!==false;
}

Returns whether there is a row of data at current position. This method is required by the interface Iterator.