Cycle ORM provides multiple options to select entity data from the database. The most common and recommended way to use the associated entity repository.
To access the repository associated with a specific entity use the method getRepository
of the orm service:
$repository = $orm->getRepository(User::class);
You can request a repository instance using the entity's class name or its role name:
$repository = $orm->getRepository("user");
The Repository provides multiple methods to select the entity.
To find the entity using its primary key:
$entity = $repository->findByPK(1);
Note, the method will return
null
if no entity is found.
To find an entity by any of its field(s) use:
$entity = $repository->findOne([
'name' => 'Antony'
]);
Field names will be automatically mapped to appropriate column names.
You can use any amount of fields in a request:
$entity = $repository->findOne([
'name' => 'Antony',
'balance' => 100
]);
If your repository is an instance of Cycle\ORM\Select\Repository
(SQL) you can also use combined expressions:
$entity = $repository->findOne([
'name' => 'Antony',
'balance' => ['>=' => 100]
]);
You can read more about compound expressions here.
To find multiple entities use:
foreach($repository->findAll(['status' => 'active']) as $e) {
// ...
}
If the repository entity is an instance of Cycle\ORM\Select\Repository
(default SQL repository) you can also access
the low level method select
, which grants you the ability to construct more complex queries or preload related
entities:
$result = $repository->select()->where('balance', '>', 1)->load('address')->fetchAll();
It's recommended to avoid usage of
select
method outside of repository classes and instead expose custom find methods.
You can read more about the methods available in select queries here.
Please note, in Cycle ORM the Repository object is only responsible for entity retrieval. All persist operations must be handled by entity manager, entity mappers and command chains.