N
N
nepster-web2018-01-11 17:27:32
Doctrine ORM
nepster-web, 2018-01-11 17:27:32

Is there a convenient solution for complex queries?

While working with Doctrine2, I noticed that it is very convenient to save entity data with its help, but you need to tinker with the selections a lot. It is clear that this is not a problem of the ORM itself, but rather a matter of approaches, and yet I would like to find a convenient solution for the formation of universal and complex queries.
So, the example will be, as always, the simplest: there are posts, tags for them, pictures and an author.
The task is also simple: get a list of categories.
And besides this, there is also a list of specific optimization and filtering cases:
- List of posts
- List of posts with pictures
- List of posts with tags
- List of posts with pictures and tags
- List of posts with pictures, tags and author
- List of posts with pictures and tags, where the author is such and such
- List of posts with pictures and tags, where the author is such and such and tag such and such
- List of posts where the number of tags is more than 5, and the author's rating is less than 100, provided that in in records more than 1 picture.
In fact, it all rests on 1 request, "getting a list of records", with filtering and conditions.
However, to implement this using doctrine2, and probably bare SQL with PHP, is quite code-intensive.
Just in case, I will give here an example of a solution that I immediately refused. This is a separate method for each request, for example:

$this->recordRepository->findList($first, $offset);
$this->recordRepository->findListWithTags($first, $offset);
$this->recordRepository->findListWithTagsAndImages($first, $offset);
$this->recordRepository->findListWithTagsAndImagesAndAuthor($first, $offset);
$this->recordRepository->findListWithTagsAndImagesAndAuthor($first, $offset);
$this->recordRepository->findListWithTagsAndImagesAndAuthorByAuthor($first, $offset, $userId);
$this->recordRepository->findListWithTagsAndImagesAndAuthorByAuthorAndTag($first, $offset, $userId, $tagId);
...

In general, there can be quite a lot of such combinations of one request. As an advantage, you can consider the convenience of testing, but the disadvantages greatly overlap this matter:
- for example, findListWithTags and findListWithTagsAndImages do not carry anything for business logic, but only internal optimization.
- the number of methods in the repository grows very strongly
- some dirt appears in the code, with the same conditions but inside.
So far my solution is something like:
$this->recordRepository->findListByCondition($condition);

Where $condition is some object that collects conditions. This approach seems to work well, but has a number of disadvantages:
- When we fill $condition with our conditions, there is a possibility of duplication in some cases.
- In the repository, you need to analyze $condition, as a result, a lot of conditions appear
- Complexity of testing.
In general, I would like to receive advice or guidance on solving this issue.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav B, 2018-01-11
@nepster-web

as a possible tip, I can throw rikbruil/doctrine-specification happyr/doctrine-specification

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question