Documentation

Deeper library knowledge

Query

Query gives you a unique way to access your stored data with united API, wherever they live. It communicates directly with adapters related to entity and the usage itself is very easy with fluent interface.

Creating queries

Queries can be created in two ways.

With builder

$queryBuilder = new UniMapper\QueryBuilder("Entity"); // Entity class, name or instance
$queryBuilder->select()-> ...

With entity

Entity::query()->select()-> ...

Executing queries

$connection = new UniMapper\Connection(new UniMapper\Mapper);
$connection->registerAdapter( ... );
Entity::query()->select()->run($connection);

Default queries

  • select ( string property1, string property2 .. ) - get all records as entity collection. You can use options selectable, limit, conditionable, sortable and cached( boolean, array options )
  • selectOne ( mixed primaryValue ) - get a single unique record by primary value. You can use option selectable.
  • insert ( array $data ) - insert a new record.
  • update ( array $data ) - update record. You can use option conditionable.
  • updateOne ( mixed primaryValue ) - update record by primary value.
  • delete - delete record. You can use option conditionable.
  • deleteOne ( mixed primaryValue ) - delete record by primary value.

Custom queries

You can even create and register your own query. This can be useful in situations such as when you need optimized query to the database and you know that it will be used in the same form several times in your code.

Definition

First of all, you need to create custom query class.

namespace MyApp\Model\Query;

class Search extends \UniMapper\Query
{
    private $pattern;

    public function __construct(\UniMapper\Reflection\Entity $entityReflection)
    {
        parent::__construct($entityReflection);
        $this->pattern = $pattern;
    }

    protected function onExecute(\UniMapper\Connection $connection)
    {
        // Do some heavy stuff with adapter and return the result
    }
}

Registration

Queries can be registered on query builder.

UniMapper\QueryBuilder::registerQuery("MyApp\Model\Query\Search");

Method name of custom query is generated from class base name, but you can change it manually by overloading method UniMapper\Query::getName(), but remember that resulted query name must be unique and should not collidate with default queries.

Useful methods

Provides common methods as Trait.

Conditionable

$query->where("propertyName", "=", 1);
$query->orWhere("propertyName", "=", 1);

Be aware of operator operator precedence (OR, AND), depending on your current adapter.

Nested conditions

$query->whereAre(function(UniMapper\Query $query) {
    $query->where(...);
});

$query->orWhereAre(....);

Operators

Selectable

  • select( string, array, ... )
  • associate ( string, array, ... )

Sortable

  • orderBy( string $name, string $direction = self::ASC )

Limit

  • limit( int )
  • offset( int )

Previous Next