Repositories are intended as a way to easily work with collections straight from the database. It allows simple methods to query for ConditionSets by abstracting away the logic of setting up a Query instance yourself, by using Model instance to set up the query.
$repository = \Parable\ORM\Repository::createForModel($model); // array of model instances $all = $repository->getAll(); // array of model instances $some = $repository->getByCondition("key", "LIKE", "value@%"); // single model instance $one = $repository->getById(1337);
It's pretty easy to get models based on certain conditions.
Repository::getByCondition() does this with a single
condition, where you pass a key, a comparator and a value. It is a shorthand way of querying based on a single
condition without having to set up a ConditionSet yourself.
||string||The key should match the property on the model (and by extension, the column name in the database).|
||string||Usually this will be
||mixed||The value to match with. In case of
Repository::getByConditionSet() is actually then called by
Repository::getByCondition() with a ConditionSet
built from the parameters you provided.
For multiple conditions, it is necessary to use ConditionSet. They're pretty straightforward, however.
$userResult = $repository->getByConditionSet( $repository->buildAndSet([ ['id', '=', $id], ['username', '=', $username], ]) );
AndSet allows simple conditions stringed together with
AND. If you want to use
OR, simply use
Limit and Order By
Repositories offer a way both limit (and use offset) and order by, just like the Query class. It simply sets
these values on the query it stores. This does mean that if you're running multiple get calls on a Repository, you have
to reset these values. This is pretty easy, however, by just calling
reset(), which resets
orderBy and the
And then you can continue with your next get call.