類別 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 |
公開方法
屬性詳細資訊
方法詳細資訊
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()");
}
建構子。
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);
}
定義於: 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);
}
定義於: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且非 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property)
時,它會被隱式呼叫。
請注意,如果屬性未定義,將傳回 false。
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;
}
定義於: 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);
}
}
定義於: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property)
時,它會被隱式呼叫。
請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,它將拋出例外。
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);
}
}
將欄位繫結到 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);
}
}
定義於: 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);
}
定義於: 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);
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
關閉讀取器。
這會釋放為執行此 SQL 語句而配置的資源。在此方法呼叫之後嘗試讀取是不可預測的。
public void close ( ) |
public function close()
{
$this->_statement->closeCursor();
$this->_closed = true;
}
傳回結果集中的資料列數量。
此方法是 Countable 介面所要求的。請注意,大多數 DBMS 可能不會給出有意義的計數。在這種情況下,請使用「SELECT COUNT(*) FROM tableName」來取得資料列數。
public integer count ( ) | ||
return | 整數 |
結果中包含的資料列數量。 |
---|
#[\ReturnTypeWillChange]
public function count()
{
return $this->getRowCount();
}
傳回目前的資料列。
此方法是 Iterator 介面所要求的。
public mixed current ( ) | ||
return | mixed |
目前的資料列。 |
---|
#[\ReturnTypeWillChange]
public function current()
{
return $this->_row;
}
傳回結果集中欄位的數量。
請注意,即使讀取器中沒有資料列,這仍然會給出正確的欄位編號。
public integer getColumnCount ( ) | ||
return | 整數 |
結果集中的欄位數量。 |
---|
public function getColumnCount()
{
return $this->_statement->columnCount();
}
讀取器是否已關閉。
public boolean getIsClosed ( ) | ||
return | 布林值 |
讀取器是否已關閉。 |
---|
public function getIsClosed()
{
return $this->_closed;
}
傳回結果集中的資料列數量。
請注意,大多數 DBMS 可能不會給出有意義的計數。在這種情況下,請使用「SELECT COUNT(*) FROM tableName」來取得資料列數。
public integer getRowCount ( ) | ||
return | 整數 |
結果中包含的資料列數量。 |
---|
public function getRowCount()
{
return $this->_statement->rowCount();
}
定義於: 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);
}
定義於: 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);
}
public void init ( ) |
public function init()
{
}
傳回目前列的索引。
此方法是 Iterator 介面所要求的。
public integer key ( ) | ||
return | 整數 |
目前資料列的索引。 |
---|
#[\ReturnTypeWillChange]
public function key()
{
return $this->_index;
}
將內部指標移動到下一列。
此方法是 Iterator 介面所要求的。
public void next ( ) |
#[\ReturnTypeWillChange]
public function next()
{
$this->_row = $this->_statement->fetch();
$this->_index++;
}
在讀取批次語句的結果時,將讀取器前進到下一個結果。
僅當查詢傳回多個結果集時,此方法才有用。並非所有 DBMS 都支援此功能。
public boolean nextResult ( ) | ||
return | 布林值 |
成功時傳回 true,失敗時傳回 false。 |
---|
public function nextResult()
{
if (($result = $this->_statement->nextRowset()) !== false) {
$this->_index = -1;
}
return $result;
}
將讀取器前進到結果集中的下一列。
public array read ( ) | ||
return | array |
目前的資料列,如果沒有更多資料列可用,則為 false |
---|
public function read()
{
return $this->_statement->fetch();
}
將整個結果集讀取到陣列中。
public array readAll ( ) | ||
return | array |
結果集(每個陣列元素代表一列資料)。如果結果不包含任何資料列,則會傳回空陣列。 |
---|
public function readAll()
{
return $this->_statement->fetchAll();
}
從結果集的下一列傳回單一欄位。
public mixed readColumn ( $columnIndex ) | ||
$columnIndex | 整數 |
從零開始的欄位索引 |
return | mixed |
目前資料列的欄位,如果沒有更多資料列可用,則為 false |
---|
public function readColumn($columnIndex)
{
return $this->_statement->fetchColumn($columnIndex);
}
傳回一個物件,其中填充了下一列資料。
public mixed readObject ( $className, $fields ) | ||
$className | string |
要建立和填充的物件的類別名稱 |
$fields | array |
此陣列的元素會傳遞至建構函式 |
return | mixed |
已填充的物件,如果沒有更多資料列可用,則為 false |
---|
public function readObject($className, $fields)
{
return $this->_statement->fetchObject($className, $fields);
}
將迭代器重設為初始狀態。
此方法是 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.');
}
}
設定此語句的預設提取模式。
另請參閱 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);
}
註冊 或 登入 以進行評論。