Class \Prado\Data\DataGateway\TTableGateway
Each method maps the input parameters into a SQL call and executes the SQL against a database connection. The TTableGateway is stateless (with respect to the data and data objects), as its role is to push data back and forth.
Example usage:
//create a connection
$dsn = 'pgsql:host=localhost;dbname=test';
$conn = new TDbConnection($dsn, 'dbuser','dbpass');
//create a table gateway for table/view named 'address'
$table = new TTableGateway('address', $conn);
//insert a new row, returns last insert id (if applicable)
$id = $table->insert(array('name'=>'wei', 'phone'=>'111111'));
$record1 = $table->findByPk($id); //find inserted record
//finds all records, returns an iterator
$records = $table->findAll();
print_r($records->readAll());
//update the row
$table->updateByPk($record1, $id);
All methods that may return more than one row of data will return an TDbDataReader iterator.
The OnCreateCommand event is raised when a command is prepared and parameter binding is completed. The parameter object is a TDataGatewayEventParameter of which the \Prado\Data\DataGateway\TDataGatewayEventParameter::getCommand property can be inspected to obtain the sql query to be executed.
The OnExecuteCommand event is raised when a command is executed and the result from the database was returned. The parameter object is a TDataGatewayResultEventParameter of which the \Prado\Data\DataGateway\TDataGatewayEventParameter::getResult property contains the data return from the database. The data returned can be changed by setting the \Prado\Data\DataGateway\TDataGatewayEventParameter::setResult property.
$table->OnCreateCommand[] = 'log_it'; //any valid PHP callback statement
$table->OnExecuteCommand[] = array($obj, 'method_name'); // calls 'method_name' on $obj
function log_it($sender, $param)
{
var_dump($param); //TDataGatewayEventParameter object.
}
Class hierarchy
Author: Wei Zhuo <weizho[at]gmail[dot]com>Since: 3.1
public
|
__call(mixed $method, mixed $args) : mixed
Dynamic find method using parts of method name as search criteria.
Method name starting with "findBy" only returns 1 record. Method name starting with "findAllBy" returns 0 or more records. Method name starting with "deleteBy" deletes records by the trail criteria. The condition is taken as part of the method name after "findBy", "findAllBy" or "deleteBy". The following are equivalent:
|
public
|
__construct(string|TDbTableInfo $table, TDbConnection $connection) : mixed
Creates a new generic table gateway for a given table or view name
and a database connection.
|
public
|
count([string|TSqlCriteria $criteria = null ][, mixed $parameters = [] ]) : int
Find the number of records.
|
public
|
deleteAll(string $criteria[, array<string|int, mixed> $parameters = [] ]) : int
Delete records from the table with condition given by $where and
binding values specified by $parameter argument.
This method uses additional arguments as $parameters. E.g.
|
public
|
|
public
|
deleteByPk(mixed $keys) : int
Delete records by primary key. Usage:
For composite primary keys (determined from the table definitions):
|
public
|
find(string|TSqlCriteria $criteria[, mixed $parameters = [] ]) : array<string|int, mixed>
Find one single record that matches the criteria.
Usage:
|
public
|
findAll([string|TSqlCriteria $criteria = null ][, mixed $parameters = [] ]) : TDbDataReader
Accepts same parameters as find(), but returns TDbDataReader instead.
|
public
|
findAllByPks(mixed $keys) : TDbDataReader
Similar to findByPk(), but returns TDbDataReader instead.
For scalar primary keys:
For composite keys:
|
public
|
findAllBySql(string $sql[, array<string|int, mixed> $parameters = [] ]) : TDbDataReader
Execute arbituary sql command with binding parameters.
|
public
|
findByPk(mixed $keys) : array<string|int, mixed>
Find one record using only the primary key or composite primary keys. Usage:
|
public
|
findBySql(string $sql[, array<string|int, mixed> $parameters = [] ]) : array<string|int, mixed>
Execute arbituary sql command with binding parameters.
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
insert(array<string|int, mixed> $data) : mixed
Inserts a new record into the table. Each array key must
correspond to a column name in the table unless a null value is permitted.
|
public
|
onCreateCommand(TDataGatewayCommand $sender, TDataGatewayEventParameter $param) : mixed
Raised when a command is prepared and parameter binding is completed.
The parameter object is TDataGatewayEventParameter of which the \Prado\Data\DataGateway\TDataGatewayEventParameter::getCommand property can be inspected to obtain the sql query to be executed. |
public
|
onExecuteCommand(TDataGatewayCommand $sender, TDataGatewayResultEventParameter $param) : mixed
Raised when a command is executed and the result from the database was returned.
The parameter object is TDataGatewayResultEventParameter of which the \Prado\Data\DataGateway\TDataGatewayEventParameter::getResult property contains the data return from the database. The data returned can be changed by setting the \Prado\Data\DataGateway\TDataGatewayEventParameter::setResult property. |
public
|
update(array<string|int, mixed> $data, string $criteria[, array<string|int, mixed> $parameters = [] ]) : int
Updates the table with new name-value pair $data. Each array key must
correspond to a column name in the table. The update condition is
specified by the $where argument and additional binding values can be
specified using the $parameter argument.
This method uses additional arguments as $parameters. E.g.
|
protected
|
|
protected
|
getCriteria(string|TSqlCriteria $criteria, mixed $parameters, array<string|int, mixed> $args) : TSqlCriteria
Create a new TSqlCriteria object from a string $criteria. The $args
are additional parameters and are used in place of the $parameters
if $parameters is not an array and $args is an arrary.
|
protected
|
|
protected
|
|
protected
|
\Prado\TComponent::GLOBAL_RAISE_EVENT_LISTENER |