Deeper library knowledge
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.
Queries can be created in two ways.
$queryBuilder = new UniMapper\QueryBuilder("Entity"); // Entity class, name or instance
$queryBuilder->select()-> ...
Entity::query()->select()-> ...
$connection = new UniMapper\Connection(new UniMapper\Mapper);
$connection->registerAdapter( ... );
Entity::query()->select()->run($connection);
string
property1, string
property2 .. ) - get all records as entity collection. You can use options selectable, limit, conditionable, sortable and cached( boolean
, array
options )mixed
primaryValue ) - get a single unique record by primary value. You can use option selectable.array
$data ) - insert a new record.array
$data ) - update record. You can use option conditionable.mixed
primaryValue ) - update record by primary value.mixed
primaryValue ) - delete record by primary value.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.
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
}
}
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.
Provides common methods as Trait.
$query->where("propertyName", "=", 1);
$query->orWhere("propertyName", "=", 1);
Be aware of operator operator precedence (OR, AND), depending on your current adapter.
$query->whereAre(function(UniMapper\Query $query) {
$query->where(...);
});
$query->orWhereAre(....);
string
, array
, ... )string
, array
, ... )string
$name, string
$direction = self::ASC )int
)int
)