4   Queries

4.1   Perform a read query

4.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:

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

    public function getUsers()
    {
        $query = $this->getQuery('SELECT ...');

        // ...

4.1.2   Parameters

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

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

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

        // ...

4.1.3   Perform the query

To fetch the data use the method query:

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

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

        // ...

4.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:

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

    public function showUsersSylvain()
    {
        $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);
        }
    }

    // ...

4.1.5   Details about the output format

See the documentation about the hydrators

4.1.6   Simplified output format for queries without joins

See the documentation about the hydrator for a single object

4.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)

4.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:

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

    public function selectOnMaster()
    {
        $query = $this->getQuery('SELECT ...');
        $query->selectMaster(true);

        // ...

4.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

4.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

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{

    public function insertSomething()
    {
        $query = $this->getQuery('INSERT INTO ...');
        $query->execute();

        // ...