Kotchasan Framework Documentation
ResultInterface
ResultInterface
\Kotchasan\Result\ResultInterface เป็น interface สำหรับ result ทุกประเภท
Interface Definition
namespace Kotchasan\Result;
interface ResultInterface
{
public function fetch();
public function fetchAll(): array;
public function fetchColumn(int $columnNumber = 0);
public function rowCount(): int;
public function columnCount(): int;
public function getColumnMeta(int $column): array;
public function getResult();
public function isEmpty(): bool;
public function hasMore(): bool;
public function reset(): bool;
public function close(): bool;
public function count(): int;
}Method Reference
fetch()
ดึงแถวถัดไปจาก result set
$result = $query->execute();
$row = $result->fetch(); // object หรือ nullfetchAll()
ดึงทุกแถวเป็น array
$result = $query->execute();
$rows = $result->fetchAll(); // array ของ objectsfetchColumn(int $columnNumber = 0)
ดึง column เดียวจากแถวถัดไป
$result = $db->select('COUNT(*)')->from('user')->execute();
$count = $result->fetchColumn(); // ค่าของ column แรกrowCount()
จำนวนแถวที่ affected (สำหรับ INSERT/UPDATE/DELETE)
$result = $db->update('user')->set(['status' => 0])->where([...])->execute();
echo $result->rowCount(); // จำนวนแถวที่ updatecolumnCount()
จำนวน columns ใน result set
$result = $db->select('id', 'name', 'email')->from('user')->execute();
echo $result->columnCount(); // 3getColumnMeta(int $column)
ดึง metadata ของ column
$meta = $result->getColumnMeta(0);
// ['name' => 'id', 'native_type' => 'LONG', ...]getResult()
ดึง underlying result object
// PDOResult: returns PDOStatement
// ArrayResult: returns array
$raw = $result->getResult();isEmpty()
ตรวจสอบว่า result ว่างหรือไม่
if ($result->isEmpty()) {
echo "No data found";
}hasMore()
ตรวจสอบว่ายังมีแถวเหลืออีกหรือไม่
while ($result->hasMore()) {
$row = $result->fetch();
}reset()
รีเซ็ต pointer กลับไปจุดเริ่มต้น
$result->fetchAll(); // อ่านหมด
$result->reset(); // รีเซ็ต
$first = $result->fetch(); // อ่านใหม่close()
ปิด cursor และ free memory
$result->close();count()
Alias ของ rowCount() (for Countable interface)
echo count($result); // ถ้า implement Countable
echo $result->count();Implementations
- PDOResult - สำหรับ PDO queries
- ArrayResult - สำหรับ cached results