Recordset
extends Query
in package
implements
Iterator
Recordset base class.
This class provides methods for querying and manipulating database records.
Tags
Interfaces, Classes and Traits
- Iterator
Table of Contents
- $cfg : object
- Config class instance.
- $db : Driver
- Database connection.
- $debugger : int
- 1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
- $request : Request
- Server request class instance.
- $sqls : array<string|int, mixed>
- ตัวแปรเก็บคำสั่ง SQL
- $datas : array<string|int, mixed>
- $field : Field
- $fields : array<string|int, mixed>
- $firstRecord : int
- $perPage : int
- $toArray : bool
- $values : array<string|int, mixed>
- __construct() : mixed
- Create a new Recordset instance.
- all() : array<string|int, mixed>|static
- Retrieve all records.
- cacheOn() : static
- Enable caching.
- count() : int
- Get the record count.
- create() : static
- Create a new instance of the Recordset class.
- createQuery() : mixed
- Build the SELECT query string.
- createTmpTable() : static
- Create a temporary table.
- createView() : bool
- Create a view based on the specified table.
- current() : mixed
- customQuery() : array<string|int, mixed>|object
- Execute a custom SQL query to retrieve data.
- db() : Driver
- Get the database connection.
- debug() : mixed
- คำสั่งสำหรับแสดง Query ออกทางหน้าจอ ใช้ในการ debug Query 1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
- delete() : bool
- Delete records based on the specified condition.
- emptyTable() : bool
- Function to delete all data in the table.
- execute() : array<string|int, mixed>|static
- Execute a query with pagination.
- fieldExists() : bool
- Check if a field exists.
- find() : Field
- Retrieve a single record by $primaryKey.
- first() : bool|array<string|int, mixed>|Field
- Query for a single record.
- getAllSettings() : object
- Get all database settings.
- getField() : Field
- Get the Field object of the Recordset.
- getFields() : array<string|int, mixed>
- Get all the field names of the Model.
- getFullTableName() : string
- ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``
- getSetting() : mixed
- Get the value of a database setting.
- getTableName() : string
- ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table
- getValues() : array<string|int, mixed>
- Get the values for execution.
- group() : string
- Function for grouping commands and connecting each group with an operator.
- insert() : int|bool
- Insert data.
- join() : static
- INNER JOIN table ON .
- key() : mixed
- Inherited from Iterator.
- limit() : static
- Limit the number of results and specify the starting record.
- next() : mixed
- Inherited from Iterator.
- order() : static
- Create a query with sorting.
- query() : bool
- Process SQL statements that do not require a result, such as CREATE, INSERT, and UPDATE.
- recordCount() : int
- Get the total number of records queried.
- rewind() : mixed
- Inherited from Iterator.
- take() : static
- Limit the number of results.
- text() : string
- ฟังก์ชั่นสร้างคำสั่ง SQL เป็นข้อความ
- toArray() : static
- Retrieve data as an array.
- toQueryBuilder() : QueryBuilder
- Export the database query as a QueryBuilder instance.
- update() : bool
- Update data.
- updateAll() : bool
- Update data for all records.
- valid() : mixed
- Determine if there are more records to iterate over.
- where() : static
- Add a WHERE clause to the query.
- aliasName() : string
- ฟังก์ชั่นสร้างคีย์ สำหรับการ execute
- buildGroup() : string
- สร้าง query สำหรับ GROUP BY
- buildJoin() : string
- สร้างคำสั่ง JOIN ถ้าไม่มี alias คืนค่าว่าง
- buildOrder() : string
- สร้าง query เรียงลำดับ
- buildSelect() : string
- ฟังก์ชั่นสร้าง query string สำหรับคำสั่ง SELECT
- buildValue() : string
- แปลงข้อมูลรูปแบบ SQL รูปแบบ array('field1', 'condition', 'field2') ไม่ระบุ condition หมายถึง = หรือ IN
- buildWhere() : string|array<string|int, mixed>
- ฟังก์ชั่นสร้างคำสั่ง WHERE คืนค่า string สำหรับคำสั่ง WHERE หรือคืนค่า array(where, values) สำหรับใช้กับการ bind
- buildWhereValues() : array<string|int, mixed>
- ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์ คืนค่า ($condition, $values)
- fieldName() : string
- แปลงข้อความสำหรับชื่อฟิลด์หรือชื่อตาราง
- fieldValue() : string
- แปลงข้อความสำหรับ value
- groupAnd() : Sql
- ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย AND
- groupOr() : Sql
- ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย OR
- quoteTableName() : string
- ฟังก์ชั่นอ่านชื่อตารางและชื่อรอง และใส่ ` ครอบชื่อตารางด้วย
- buildQuery() : mixed
- Build a query based on the provided configuration.
- doExecute() : array<string|int, mixed>|static
- Execute a query with pagination.
- doJoin() : static
- Perform a JOIN operation on the query.
- whereValue() : array<string|int, mixed>|string
- สร้างคำสั่ง WHERE
Properties
$cfg
Config class instance.
protected
static object
$cfg
$db
Database connection.
protected
Driver
$db
$debugger
1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
protected
int
$debugger
= 0
$request
Server request class instance.
protected
static Request
$request
$sqls
ตัวแปรเก็บคำสั่ง SQL
protected
array<string|int, mixed>
$sqls
$datas
private
array<string|int, mixed>
$datas
The data records.
$field
private
Field
$field
The field class instance.
$fields
private
array<string|int, mixed>
$fields
= []
The field names.
$firstRecord
private
int
$firstRecord
The starting record for pagination.
$perPage
private
int
$perPage
The number of records per page for pagination.
$toArray
private
bool
$toArray
= false
Determines the type of result: true for array, false for model.
$values
private
array<string|int, mixed>
$values
If values are set, it will use prepare instead of execute.
Methods
__construct()
Create a new Recordset instance.
public
__construct(string $field) : mixed
Parameters
- $field : string
-
The name of the Field class.
Return values
mixed —all()
Retrieve all records.
public
all([array<string|int, mixed>|string|null $fields = null ]) : array<string|int, mixed>|static
Executes the SELECT query and returns the result as an array or model objects.
Parameters
- $fields : array<string|int, mixed>|string|null = null
-
(optional) The fields to select. Default is null (select all fields).
Return values
array<string|int, mixed>|static —The result as an array or model objects.
cacheOn()
Enable caching.
public
cacheOn([bool $auto_save = true ]) : static
Enables caching and specifies whether to automatically save the cache.
Parameters
- $auto_save : bool = true
-
(optional) Specifies whether to automatically save the cache. Default is true.
Return values
static —count()
Get the record count.
public
count() : int
Executes a COUNT query and returns the number of records matching the conditions.
Return values
int —The number of records matching the conditions.
create()
Create a new instance of the Recordset class.
public
static create(string $field) : static
Parameters
- $field : string
-
The name of the Field class.
Return values
static —A new instance of the Recordset class.
createQuery()
Build the SELECT query string.
public
createQuery(mixed $start, mixed $count) : mixed
Parameters
- $start : mixed
- $count : mixed
Return values
mixed —createTmpTable()
Create a temporary table.
public
createTmpTable(string $table) : static
Parameters
- $table : string
-
The table name.
Return values
static —createView()
Create a view based on the specified table.
public
createView(string $table) : bool
Parameters
- $table : string
-
The name of the table.
Return values
bool —True if the view is created successfully, false otherwise.
current()
public
current() : mixed
Return values
mixed —customQuery()
Execute a custom SQL query to retrieve data.
public
customQuery(string $sql[, bool $toArray = true ][, array<string|int, mixed> $values = [] ]) : array<string|int, mixed>|object
Returns the result as an array or object based on the $toArray parameter.
Parameters
- $sql : string
-
The query string.
- $toArray : bool = true
-
(optional) True to return the result as an array, false to return as an object. Default is true.
- $values : array<string|int, mixed> = []
-
(optional) If specified, it will use prepared statements instead of direct query execution.
Return values
array<string|int, mixed>|object —The result data.
db()
Get the database connection.
public
db() : Driver
Return values
Driver —The database connection.
debug()
คำสั่งสำหรับแสดง Query ออกทางหน้าจอ ใช้ในการ debug Query 1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
public
debug([int $value = 1 ]) : mixed
Parameters
- $value : int = 1
Return values
mixed —delete()
Delete records based on the specified condition.
public
delete([mixed $condition = [] ][, bool $all = false ][, string $operator = 'AND' ]) : bool
Returns true if successful.
Parameters
- $condition : mixed = []
-
The condition for deletion. It can be an int (primaryKey), string (SQL QUERY), or array.
- $all : bool = false
-
(optional) If false (default), delete a single record. If true, delete all records that match the condition.
- $operator : string = 'AND'
-
(optional) The operator to join each $condition. Use 'AND' (default) or 'OR'.
Return values
bool —True if the deletion is successful, false otherwise.
emptyTable()
Function to delete all data in the table.
public
emptyTable() : bool
Returns true if successful.
Return values
bool —True if the operation is successful, false otherwise.
execute()
Execute a query with pagination.
public
execute([array<string|int, mixed>|string $fields = null ]) : array<string|int, mixed>|static
SELECT ...
Parameters
- $fields : array<string|int, mixed>|string = null
-
(optional) If null, SELECT all fields. Otherwise, specify the fields to select.
Return values
array<string|int, mixed>|static —fieldExists()
Check if a field exists.
public
fieldExists(string $field) : bool
Returns true if the field exists, false otherwise.
Parameters
- $field : string
-
The field name.
Return values
bool —True if the field exists, false otherwise.
find()
Retrieve a single record by $primaryKey.
public
find(int $id) : Field
Parameters
- $id : int
-
The ID of the record to retrieve.
Return values
Field —The record of the specified ID.
first()
Query for a single record.
public
first([array<string|int, mixed>|string $fields = null ]) : bool|array<string|int, mixed>|Field
Returns false if not found, the record data if found. SELECT .... LIMIT 1
Parameters
- $fields : array<string|int, mixed>|string = null
-
(optional) If null, SELECT all fields. Otherwise, specify the fields to select.
Return values
bool|array<string|int, mixed>|Field —False if not found, the record data if found.
getAllSettings()
Get all database settings.
public
getAllSettings() : object
Return values
object —The database settings object.
getField()
Get the Field object of the Recordset.
public
getField() : Field
Return values
Field —The Field object.
getFields()
Get all the field names of the Model.
public
getFields() : array<string|int, mixed>
Return values
array<string|int, mixed> —The array of field names.
getFullTableName()
ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``
public
getFullTableName(string $table) : string
Parameters
- $table : string
-
ชื่อตารางตามที่กำหนดใน settings/datasbase.php
Return values
string —getSetting()
Get the value of a database setting.
public
getSetting(string $key) : mixed
Parameters
- $key : string
-
The setting key.
Return values
mixed —The value of the setting.
getTableName()
ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table
public
getTableName(string $table) : string
Parameters
- $table : string
-
ชื่อตารางตามที่กำหนดใน settings/datasbase.php
Return values
string —getValues()
Get the values for execution.
public
getValues() : array<string|int, mixed>
Return values
array<string|int, mixed> —The values for execution.
group()
Function for grouping commands and connecting each group with an operator.
public
group(array<string|int, mixed> $params[, string $operator = 'AND' ]) : string
Returns the grouped query within ().
Parameters
- $params : array<string|int, mixed>
-
The commands in the format array('field1', 'condition', 'field2').
- $operator : string = 'AND'
-
The operator (AND or OR).
Return values
string —The grouped query.
insert()
Insert data.
public
insert(Field $field) : int|bool
Returns the ID of the inserted data on success, false on failure.
Parameters
- $field : Field
-
The field object to be inserted.
Return values
int|bool —The ID of the inserted data if successful, false if an error occurred.
join()
INNER JOIN table ON .
public
join(string $field, string $type, mixed $on) : static
..
Parameters
- $field : string
-
The field class of the table to join.
- $type : string
-
The type of join (e.g., LEFT, RIGHT, INNER...).
- $on : mixed
-
The join condition.
Return values
static —key()
Inherited from Iterator.
public
key() : mixed
Return values
mixed —limit()
Limit the number of results and specify the starting record.
public
limit(int $count, int $start) : static
Parameters
- $count : int
-
The number of results to limit.
- $start : int
-
The starting record.
Return values
static —next()
Inherited from Iterator.
public
next() : mixed
Return values
mixed —order()
Create a query with sorting.
public
order(mixed $sorts) : static
Parameters
- $sorts : mixed
-
An array of sort orders ('field ASC', 'field DESC') or multiple arguments 'field ASC', 'field DESC', ...
Return values
static —query()
Process SQL statements that do not require a result, such as CREATE, INSERT, and UPDATE.
public
query(string $sql[, array<string|int, mixed> $values = [] ]) : bool
Returns true on success, false on failure.
Parameters
- $sql : string
-
The SQL statement to be processed.
- $values : array<string|int, mixed> = []
-
If specified, it forces the use of prepared statements instead of query.
Return values
bool —True if successful, false if an error occurred.
recordCount()
Get the total number of records queried.
public
recordCount() : int
Return values
int —The total number of records.
rewind()
Inherited from Iterator.
public
rewind() : mixed
Return values
mixed —take()
Limit the number of results.
public
take() : static
LIMIT $start, $count.
Return values
static —text()
ฟังก์ชั่นสร้างคำสั่ง SQL เป็นข้อความ
public
text() : string
Return values
string —toArray()
Retrieve data as an array.
public
toArray() : static
This function should be called before querying data.
Return values
static —toQueryBuilder()
Export the database query as a QueryBuilder instance.
public
toQueryBuilder() : QueryBuilder
Return values
QueryBuilder —The QueryBuilder instance.
update()
Update data.
public
update(array<string|int, mixed> $condition, array<string|int, mixed>|Field $save) : bool
Parameters
- $condition : array<string|int, mixed>
-
The condition for the update.
- $save : array<string|int, mixed>|Field
-
The data to be saved.
Return values
bool —True if successful, false if an error occurred.
updateAll()
Update data for all records.
public
updateAll(array<string|int, mixed> $save) : bool
Parameters
- $save : array<string|int, mixed>
-
The data to be saved (array('key1' => 'value1', 'key2' => 'value2', ...)).
Return values
bool —True if successful, false if an error occurred.
valid()
Determine if there are more records to iterate over.
public
valid() : mixed
Return values
mixed —The validity of the current record.
where()
Add a WHERE clause to the query.
public
where([mixed $where = [] ][, string $operator = 'AND' ]) : static
Parameters
- $where : mixed = []
-
The condition(s) for the WHERE clause.
- $operator : string = 'AND'
-
(optional) The operator to use for multiple conditions (AND by default).
Return values
static —The updated Recordset instance.
aliasName()
ฟังก์ชั่นสร้างคีย์ สำหรับการ execute
protected
aliasName(string $name[, string $prefix = '' ]) : string
Parameters
- $name : string
-
ชื่อฟิลด์
- $prefix : string = ''
-
คำนำหน้าชื่อฟิลด์ ใช้เพื่อป้องกันการใช้ตัวแปรซ้ำ
Return values
string —buildGroup()
สร้าง query สำหรับ GROUP BY
protected
buildGroup(array<string|int, mixed>|string $fields) : string
Parameters
- $fields : array<string|int, mixed>|string
-
array('U.id', 'U.username') หรือ string U.id
Return values
string —buildJoin()
สร้างคำสั่ง JOIN ถ้าไม่มี alias คืนค่าว่าง
protected
buildJoin(string|array<string|int, mixed> $table, string $type, mixed $on) : string
Parameters
- $table : string|array<string|int, mixed>
-
ชื่อตารางต้องมี alias ด้วย หรือ (QueryBuilder, alias)
- $type : string
-
เข่น INNER OUTER LEFT RIGHT
- $on : mixed
-
query string หรือ array
Return values
string —buildOrder()
สร้าง query เรียงลำดับ
protected
buildOrder(array<string|int, mixed>|string $fields) : string
Parameters
- $fields : array<string|int, mixed>|string
-
array('field ASC','field DESC') หรือ 'field ASC', 'field DESC', ...
Return values
string —buildSelect()
ฟังก์ชั่นสร้าง query string สำหรับคำสั่ง SELECT
protected
buildSelect(string|array<string|int, mixed>|QueryBuilder $fields, mixed $alias) : string
Parameters
- $fields : string|array<string|int, mixed>|QueryBuilder
- $alias : mixed
Return values
string —buildValue()
แปลงข้อมูลรูปแบบ SQL รูปแบบ array('field1', 'condition', 'field2') ไม่ระบุ condition หมายถึง = หรือ IN
protected
buildValue(array<string|int, mixed> $params) : string
Parameters
- $params : array<string|int, mixed>
Return values
string —buildWhere()
ฟังก์ชั่นสร้างคำสั่ง WHERE คืนค่า string สำหรับคำสั่ง WHERE หรือคืนค่า array(where, values) สำหรับใช้กับการ bind
protected
buildWhere(mixed $condition[, string $operator = 'AND' ][, string $id = 'id' ]) : string|array<string|int, mixed>
Parameters
- $condition : mixed
- $operator : string = 'AND'
-
(optional) เช่น AND หรือ OR
- $id : string = 'id'
-
(optional )ชื่อฟิลด์ที่เป็น key
Return values
string|array<string|int, mixed> —buildWhereValues()
ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์ คืนค่า ($condition, $values)
protected
buildWhereValues(mixed $condition[, string $operator = 'AND' ][, string $id = 'id' ]) : array<string|int, mixed>
Parameters
- $condition : mixed
- $operator : string = 'AND'
-
(optional) เช่น AND หรือ OR
- $id : string = 'id'
-
(optional )ชื่อฟิลด์ที่เป็น key
Return values
array<string|int, mixed> —fieldName()
แปลงข้อความสำหรับชื่อฟิลด์หรือชื่อตาราง
protected
fieldName(string $name) : string
Parameters
- $name : string
Return values
string —fieldValue()
แปลงข้อความสำหรับ value
protected
fieldValue(string $value) : string
Parameters
- $value : string
Return values
string —groupAnd()
ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย AND
protected
groupAnd(array<string|int, mixed> $params) : Sql
Parameters
- $params : array<string|int, mixed>
-
คำสั่ง รูปแบบ array('field1', 'condition', 'field2')
Return values
Sql —groupOr()
ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย OR
protected
groupOr(array<string|int, mixed> $params) : Sql
Parameters
- $params : array<string|int, mixed>
-
คำสั่ง รูปแบบ array('field1', 'condition', 'field2')
Return values
Sql —quoteTableName()
ฟังก์ชั่นอ่านชื่อตารางและชื่อรอง และใส่ ` ครอบชื่อตารางด้วย
protected
quoteTableName(string $table) : string
Parameters
- $table : string
-
ชื่อตารางตามที่กำหนดใน settings/datasbase.php
Return values
string —buildQuery()
Build a query based on the provided configuration.
private
buildQuery(string $method, mixed $param) : mixed
Parameters
- $method : string
-
The query method to build.
- $param : mixed
-
The parameters for the query.
Return values
mixed —doExecute()
Execute a query with pagination.
private
doExecute(int $start, int $end) : array<string|int, mixed>|static
Parameters
- $start : int
-
The start index of the query results.
- $end : int
-
The end index of the query results.
Return values
array<string|int, mixed>|static —The query results or the updated Recordset instance.
doJoin()
Perform a JOIN operation on the query.
private
doJoin(string $field, string $type, string|array<string|int, mixed> $on) : static
Parameters
- $field : string
-
The name of the field or table to join.
- $type : string
-
The type of join operation (e.g., INNER, LEFT, RIGHT).
- $on : string|array<string|int, mixed>
-
The join condition(s).
Return values
static —The updated Recordset instance.
whereValue()
สร้างคำสั่ง WHERE
private
whereValue(array<string|int, mixed> $params[, int|null $i = null ]) : array<string|int, mixed>|string
Parameters
- $params : array<string|int, mixed>
- $i : int|null = null