0 追蹤者

類別 yii\db\DataReader

繼承關係yii\db\DataReader » yii\base\BaseObject
實作介面Countable, Iterator, yii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/DataReader.php

DataReader 代表來自查詢結果集的一組唯讀資料列串流。

若要讀取目前的資料列,請呼叫 read() 方法。readAll() 方法會以單一陣列傳回所有資料列。資料列也可以透過迭代讀取器來讀取。例如:

$command = $connection->createCommand('SELECT * FROM post');
$reader = $command->query();

while ($row = $reader->read()) {
    $rows[] = $row;
}

// equivalent to:
foreach ($reader as $row) {
    $rows[] = $row;
}

// equivalent to:
$rows = $reader->readAll();

請注意,由於 DataReader 是唯讀串流,您只能遍歷一次。第二次執行會拋出例外。

可以透過設定 $fetchMode 來使用特定的資料提取模式。有關可能的提取模式的更多詳細資訊,請參閱 PHP 手冊

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$columnCount 整數 結果集中的欄位數量。 yii\db\DataReader
$fetchMode 整數 提取模式。 yii\db\DataReader
$isClosed 布林值 讀取器是否已關閉。 yii\db\DataReader
$rowCount 整數 結果中包含的資料列數量。 yii\db\DataReader

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\BaseObject
__construct() 建構子。 yii\db\DataReader
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
bindColumn() 將欄位繫結到 PHP 變數。 yii\db\DataReader
canGetProperty() 傳回一個值,指示屬性是否可讀取。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示屬性是否可設定。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
close() 關閉讀取器。 yii\db\DataReader
count() 傳回結果集中的資料列數量。 yii\db\DataReader
current() 傳回目前的資料列。 yii\db\DataReader
getColumnCount() 傳回結果集中欄位的數量。 yii\db\DataReader
getIsClosed() 讀取器是否已關閉。 yii\db\DataReader
getRowCount() 傳回結果集中的資料列數量。 yii\db\DataReader
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
key() 傳回目前列的索引。 yii\db\DataReader
next() 將內部指標移動到下一列。 yii\db\DataReader
nextResult() 在讀取批次語句的結果時,將讀取器前進到下一個結果。 yii\db\DataReader
read() 將讀取器前進到結果集中的下一列。 yii\db\DataReader
readAll() 將整個結果集讀取到陣列中。 yii\db\DataReader
readColumn() 從結果集的下一列傳回單一欄位。 yii\db\DataReader
readObject() 傳回一個物件,其中填充了下一列資料。 yii\db\DataReader
rewind() 將迭代器重設為初始狀態。 yii\db\DataReader
setFetchMode() 設定此語句的預設提取模式。 yii\db\DataReader
valid() 傳回目前位置是否有資料列。 yii\db\DataReader

屬性詳細資訊

隱藏繼承的屬性

$columnCount 公開屬性

結果集中的欄位數量。

public integer $columnCount null
$fetchMode 公開屬性

提取模式。

public integer $fetchMode null
$isClosed 公開屬性

讀取器是否已關閉。

public boolean $isClosed null
$rowCount 公開屬性

結果中包含的資料列數量。

public integer $rowCount null

方法詳細資訊

隱藏繼承的方法

__call() 公開方法

定義於: yii\base\BaseObject::__call()

呼叫指定的非類別方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當未知方法被調用時,它會被隱式呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() 公開方法

建構子。

public void __construct ( yii\db\Command $command, $config = [] )
$command yii\db\Command

產生查詢結果的命令

$config array

將用於初始化物件屬性的名稱-值對

                public function __construct(Command $command, $config = [])
{
    $this->_statement = $command->pdoStatement;
    $this->_statement->setFetchMode(\PDO::FETCH_ASSOC);
    parent::__construct($config);
}

            
__get() 公開方法

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯寫

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() 公開方法

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,將傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return 布林值

具名屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() 公開方法

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯讀

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() 公開方法

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
bindColumn() 公開方法

將欄位繫結到 PHP 變數。

當提取資料列時,對應的欄位值將設定在變數中。請注意,提取模式必須包含 PDO::FETCH_BOUND。

另請參閱 https://php.dev.org.tw/manual/en/function.PDOStatement-bindColumn.php

public void bindColumn ( $column, &$value, $dataType null )
$column integer|string

結果集中的欄位編號(從 1 開始索引)或欄位名稱。如果使用欄位名稱,請注意名稱應與驅動程式傳回的欄位大小寫相符。

$value mixed

PHP 變數的名稱,欄位將綁定到該變數。

$dataType integer|null

參數的資料類型

                public function bindColumn($column, &$value, $dataType = null)
{
    if ($dataType === null) {
        $this->_statement->bindColumn($column, $value);
    } else {
        $this->_statement->bindColumn($column, $value, $dataType);
    }
}

            
canGetProperty() 公開方法

定義於: yii\base\BaseObject::canGetProperty()

傳回一個值,指示屬性是否可讀取。

如果屬性可讀取,則滿足以下條件:

  • 類別具有與指定名稱相關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

屬性是否可讀取

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() 公開方法

定義於: yii\base\BaseObject::canSetProperty()

傳回一個值,指示屬性是否可設定。

如果屬性可寫入,則滿足以下條件:

  • 類別具有與指定名稱相關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

屬性是否可寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() 公開靜態方法
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
close() 公開方法

關閉讀取器。

這會釋放為執行此 SQL 語句而配置的資源。在此方法呼叫之後嘗試讀取是不可預測的。

public void close ( )

                public function close()
{
    $this->_statement->closeCursor();
    $this->_closed = true;
}

            
count() 公開方法

傳回結果集中的資料列數量。

此方法是 Countable 介面所要求的。請注意,大多數 DBMS 可能不會給出有意義的計數。在這種情況下,請使用「SELECT COUNT(*) FROM tableName」來取得資料列數。

public integer count ( )
return 整數

結果中包含的資料列數量。

                #[\ReturnTypeWillChange]
public function count()
{
    return $this->getRowCount();
}

            
current() 公開方法

傳回目前的資料列。

此方法是 Iterator 介面所要求的。

public mixed current ( )
return mixed

目前的資料列。

                #[\ReturnTypeWillChange]
public function current()
{
    return $this->_row;
}

            
getColumnCount() 公開方法

傳回結果集中欄位的數量。

請注意,即使讀取器中沒有資料列,這仍然會給出正確的欄位編號。

public integer getColumnCount ( )
return 整數

結果集中的欄位數量。

                public function getColumnCount()
{
    return $this->_statement->columnCount();
}

            
getIsClosed() 公開方法

讀取器是否已關閉。

public boolean getIsClosed ( )
return 布林值

讀取器是否已關閉。

                public function getIsClosed()
{
    return $this->_closed;
}

            
getRowCount() 公開方法

傳回結果集中的資料列數量。

請注意,大多數 DBMS 可能不會給出有意義的計數。在這種情況下,請使用「SELECT COUNT(*) FROM tableName」來取得資料列數。

public integer getRowCount ( )
return 整數

結果中包含的資料列數量。

                public function getRowCount()
{
    return $this->_statement->rowCount();
}

            
hasMethod() 公開方法

定義於: yii\base\BaseObject::hasMethod()

傳回一個值,指示是否已定義方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return 布林值

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() 公開方法

定義於: yii\base\BaseObject::hasProperty()

傳回一個值,指示是否已定義屬性。

如果滿足以下條件,則屬性已定義:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() 公開方法

定義於: yii\base\BaseObject::init()

初始化物件。

在物件使用給定的配置初始化後,在建構函式的結尾會調用此方法。

public void init ( )

                public function init()
{
}

            
key() 公開方法

傳回目前列的索引。

此方法是 Iterator 介面所要求的。

public integer key ( )
return 整數

目前資料列的索引。

                #[\ReturnTypeWillChange]
public function key()
{
    return $this->_index;
}

            
next() 公開方法

將內部指標移動到下一列。

此方法是 Iterator 介面所要求的。

public void next ( )

                #[\ReturnTypeWillChange]
public function next()
{
    $this->_row = $this->_statement->fetch();
    $this->_index++;
}

            
nextResult() 公開方法

在讀取批次語句的結果時,將讀取器前進到下一個結果。

僅當查詢傳回多個結果集時,此方法才有用。並非所有 DBMS 都支援此功能。

public boolean nextResult ( )
return 布林值

成功時傳回 true,失敗時傳回 false。

                public function nextResult()
{
    if (($result = $this->_statement->nextRowset()) !== false) {
        $this->_index = -1;
    }
    return $result;
}

            
read() 公開方法

將讀取器前進到結果集中的下一列。

public array read ( )
return array

目前的資料列,如果沒有更多資料列可用,則為 false

                public function read()
{
    return $this->_statement->fetch();
}

            
readAll() 公開方法

將整個結果集讀取到陣列中。

public array readAll ( )
return array

結果集(每個陣列元素代表一列資料)。如果結果不包含任何資料列,則會傳回空陣列。

                public function readAll()
{
    return $this->_statement->fetchAll();
}

            
readColumn() 公開方法

從結果集的下一列傳回單一欄位。

public mixed readColumn ( $columnIndex )
$columnIndex 整數

從零開始的欄位索引

return mixed

目前資料列的欄位,如果沒有更多資料列可用,則為 false

                public function readColumn($columnIndex)
{
    return $this->_statement->fetchColumn($columnIndex);
}

            
readObject() 公開方法

傳回一個物件,其中填充了下一列資料。

public mixed readObject ( $className, $fields )
$className string

要建立和填充的物件的類別名稱

$fields array

此陣列的元素會傳遞至建構函式

return mixed

已填充的物件,如果沒有更多資料列可用,則為 false

                public function readObject($className, $fields)
{
    return $this->_statement->fetchObject($className, $fields);
}

            
rewind() 公開方法

將迭代器重設為初始狀態。

此方法是 Iterator 介面所要求的。

public void rewind ( )
throws yii\base\InvalidCallException

如果此方法被調用兩次

                #[\ReturnTypeWillChange]
public function rewind()
{
    if ($this->_index < 0) {
        $this->_row = $this->_statement->fetch();
        $this->_index = 0;
    } else {
        throw new InvalidCallException('DataReader cannot rewind. It is a forward-only reader.');
    }
}

            
setFetchMode() 公有方法

設定此語句的預設提取模式。

另請參閱 https://php.dev.org.tw/manual/en/function.PDOStatement-setFetchMode.php

public void setFetchMode ( $mode )
$mode 整數

提取模式

                public function setFetchMode($mode)
{
    $params = func_get_args();
    call_user_func_array([$this->_statement, 'setFetchMode'], $params);
}

            
valid() 公有方法

傳回目前位置是否有資料列。

此方法是 Iterator 介面所要求的。

public boolean valid ( )
return 布林值

目前位置是否有資料列。

                #[\ReturnTypeWillChange]
public function valid()
{
    return $this->_row !== false;
}