1   Queries

1.1   Perform a read query

1.1.1   The Query object

A query is done by retrieving the object CCMBenchmark\Ting\Query\Query from the Repository.

To do this, just call:

$query = $this->getQuery('SELECT ...');

1.1.2   Parameters

To define dynamic parameters in the query call the method “setParams” which takes as argument an associative array:

$query = $this->getQuery('SELECT id, name FROM user WHERE name = :name');
$query->setParams(['name' => 'Sylvain']);

1.1.3   Perform the query

To fetch the data use the method “query”:

$query = $this->getQuery('SELECT id, name FROM user WHERE name = :name');
$query->setParams(['name' => 'Sylvain']);
$collection = $query->query();

1.1.4   Get the output

The “query” method returns an object CCMBenchmark\Ting\Repository\Collection. This object is an Iterator, you can retrieve the results with an iteration:

$query = $this->getQuery('SELECT id, name FROM user WHERE name = :name');
$query->setParams(['name' => 'Sylvain']);
$collection = $query->query();
foreach ($collection as $data) {
    print_r($data);
}

1.1.5   Details about the output format

See the documentation about the hydrators

1.1.6   Simplified output format for queries without joins

See the documentation about the hydrator for a single object

1.1.7   Counting items in a collection

Knowing that a Collection implements the standard interface Countable. You can find how many elements it contains just as you would do for a PHP array: count($collection);

1.1.8   Perform a query on the master

A read query will be made by default on one of the configured slaves (when some slaves have been configured), if you need to perform your query on the master, then you must call the method “selectMaster”:

$query = $this->getQuery('SELECT ...');
$query->selectMaster(true);

1.1.9   Error on a query

If the query executed does not complete successfully, an exception of type CCMBenchmark\Ting\Driver\QueryException will be raised

1.2   Perform a write query

The logic is the same as for a read request. Except that you need to call the method “execute” on the object CCMBenchmark\Ting\Query\Query

$query = $this->getQuery('INSERT INTO ...');
$query->execute();