Because each Query object extends its relevant Atlas.Statement class, the Statement methods are available to build the Query statement.
Thus, by using a Query, you can both build and execute the statement with a single object.
After you build a SELECT statement,
call the perform()
method to execute it and get back a PDOStatement.
$pdoStatement = $select->perform();
The Select proxies all fetch*()
and yield()
method calls to the underlying
Connection object via the magic __call()
method:
fetchAll() : array|false
fetchAffected() : int
fetchColumn(int $column = 0) : array|false
fetchGroup(int $style = PDO::FETCH_COLUMN) : array|false
fetchKeyPair() : array|false
fetchObject(string $class = 'stdClass', array $args = []) : object|false
fetchObjects(string $class = 'stdClass', array $args = []) : array|false
fetchOne() : array|false
fetchUnique() : array|false
fetchValue() : mixed
yieldAll() : Generator
yieldColumn(int $column = 0) : Generator
yieldKeyPair() : Generator
yieldObjects(string $class = 'stdClass', array $args = []) : Generator
yieldUnique() : Generator
For example, to build a query and get back an array of all results:
// SELECT * FROM foo WHERE bar > :_1_1_
$result = $select
->columns('*')
->from('foo')
->where('bar > ', $value)
->fetchAll();
foreach ($result as $key => $val) {
echo $val['bar'] . PHP_EOL;
}
For more information on the fetch*()
and yield*()
methods, please see the
Atlas.Pdo Connection
documentation.
After you build an INSERT statement,
call the perform()
method to execute it and get back a PDOStatement.
$pdoStatement = $insert->perform();
If the database autoincrements a column while performing the query, you can get
back that value using the getLastInsertId()
method:
$id = $insert->getLastInsertId();
Note:
You can pass a sequence name as an optional parameter to
getLastInsertId()
; this may be required with PostgreSQL.
If you added a RETURNING
clause with the returning()
method, you can
retrieve those column values with the returned PDOStatement:
$pdoStatement = $insert->perform();
$values = $pdoStatement->fetch(); // : array
After you build an UPDATE statement,
call the perform()
method to execute it and get back a PDOStatement.
$pdoStatement = $update->perform();
If you added a RETURNING
clause with the returning()
method, you can
retrieve those column values with the returned PDOStatement:
$pdoStatement = $update->perform();
$values = $pdoStatement->fetch(); // : array
If you added a RETURNING
clause with the returning()
method, you can
retrieve those column values with the returned PDOStatement:
$pdoStatement = $update->perform();
$values = $pdoStatement->fetch(); // : array
After you build a DELETE statement,
call the perform()
method to execute it and get back a PDOStatement.
$pdoStatement = $delete->perform(); // : PDOStatement