murph-doc/docs/entities/query.md

126 lines
2.7 KiB
Markdown
Raw Normal View History

2021-05-31 16:30:46 +02:00
# Repository Query
A Repository query is an abstraction of the doctrine repository.
2021-06-04 10:48:41 +02:00
## Requirement
2021-05-31 16:30:46 +02:00
Entities must implements `App\Core\Entity\EntityInterface`.
2023-02-09 21:50:06 +01:00
```php-inline title="src/Entity/MyEntity.php"
2021-05-31 16:30:46 +02:00
namespace App\Entity;
use App\Repository\MyEntityRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Core\Entity\EntityInterface;
/**
* @ORM\Entity(repositoryClass=MyEntityRepository::class)
*/
class MyEntity implements EntityInterface
{
// ...
}
```
2021-06-04 10:48:41 +02:00
## Generation
The generation is performed in CLI. These information are required:
* The namespace of the repository (eg: `MyEntityRepository`)
Simply run `php bin/console make:repository-query`.
2021-05-31 16:30:46 +02:00
Each entity has its own repository query which is a service.
2021-06-04 10:48:41 +02:00
2023-02-09 21:50:06 +01:00
```php-inline title="src/Repository/MyEntityRepositoryQuery"
2021-05-31 16:30:46 +02:00
namespace App\Repository;
use App\Core\Repository\RepositoryQuery;
use Knp\Component\Pager\PaginatorInterface;
class MyEntityRepositoryQuery extends RepositoryQuery
{
public function __construct(MyEntityRepository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'm', $paginator);
}
// Example of custom filter
public function filterByFooBar(bool $foo, bool $bar): self
{
$this
->andWhere('.foo = :foo')
->andWhere('.bar = :bar')
->setParameter(':foo', $foo)
->setParameter(':bar', $bar);
return $this;
}
}
```
## Usage
You are able to find entities in an easy way, without knowing the identification variable and without creating a query builder.
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/FooController.php"
2021-05-31 16:30:46 +02:00
namespace App\Controller;
use App\Repository\MyEntityRepositoryQuery
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(MyEntityRepositoryQuery $query): Response
{
$entities = $query->create()->find();
$entity = $query->create()->findOne();
// Filtered and sorted entities
$entities = $query->create()
->orderBy('.id', 'DESC')
->where('.isPublished = true')
->find();
// ...
}
}
```
2021-06-04 10:48:41 +02:00
## Custom methods
2023-02-09 21:50:06 +01:00
```php-inline
2021-06-04 10:48:41 +02:00
// ...
class MyEntityRepositoryQuery extends RepositoryQuery
{
// ...
public function filterByFooBar(bool $foo, bool $bar): self
{
$this
->andWhere('.foo = :foo')
->andWhere('.bar = :bar')
->setParameter(':foo', $foo)
->setParameter(':bar', $bar);
return $this;
}
}
```
2021-05-31 16:30:46 +02:00
2023-02-09 21:50:06 +01:00
```php-inline
2021-05-31 16:30:46 +02:00
$entities = $query->create()
->filterByFoo($foo, $bar)
->find();
```
## Pager
You can paginate entities (`Knp\Component\Pager\Pagination\PaginationInterface`):
2023-02-09 21:50:06 +01:00
```php-inline
2021-05-31 16:30:46 +02:00
$pager = $query->create()->paginate($page, $maxPerPage);
```