murph-doc/docs/entities/query.md

178 lines
4.1 KiB
Markdown
Raw Permalink 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;
2023-03-10 16:29:39 +01:00
#[ORM\Entity(repositoryClass: MyEntityRepository::class)]
2021-05-31 16:30:46 +02:00
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();
```
2023-04-09 21:16:27 +02:00
In the context of a CRUD, filters are applied using [the method `useFilters`](https://gitnet.fr/murph/murph-core/src/branch/master/src/core/Repository/RepositoryQuery.php#L78-L99).
Integers, strings and booleans are automatically processed. Other types are passed to [the method `filterHandler`](https://gitnet.fr/murph/murph-core/src/branch/master/src/core/Repository/RepositoryQuery.php#L122-L124).
You have to override it to manage them, example:
```php-inline
// ...
use App\Entity\Something;
class MyEntityRepositoryQuery extends RepositoryQuery
{
// ...
public function filterHandler(string $name, $value): self
{
if ($name === 'something' && $value instanceof Something) {
$this
->join('something', 's')
->andWhere('s.id = :something')
->setParameter('something', $value->getId())
;
}
}
}
```
2023-10-11 23:08:48 +02:00
You can also force `filterHandler` te be used for specific filter field:
```php-inline
// ...
class MyEntityRepositoryQuery extends RepositoryQuery
{
public function __construct(Repository $repository, PaginatorInterface $paginator)
{
// ...
$this->addForcedFilterHandler('foo');
}
public function filterHandler(string $name, $value): self
{
// ...
if ($name === 'foo) {
// ...
}
}
}
```
2023-04-09 21:16:27 +02:00
2021-05-31 16:30:46 +02:00
## 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);
```