1.3.2. Query Execution

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.

1.3.2.1. SELECT

After you build a SELECT statement, call the perform() method to execute it and get back a PDOStatement.

$pdoStatement = $select->perform();

1.3.2.1.1. Fetching and Yielding

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.

1.3.2.2. INSERT

After you build an INSERT statement, call the perform() method to execute it and get back a PDOStatement.

$pdoStatement = $insert->perform();

1.3.2.2.1. Last Insert ID

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.

1.3.2.2.2. RETURNING

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

1.3.2.3. UPDATE

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

1.3.2.3.1. RETURNING

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

1.3.2.4. DELETE

After you build a DELETE statement, call the perform() method to execute it and get back a PDOStatement.

$pdoStatement = $delete->perform(); // : PDOStatement