{"id":355714,"date":"2024-05-20T23:14:32","date_gmt":"2024-05-20T23:14:32","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=355714"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-20T23:14:32","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=355714","title":{"rendered":"<span>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0432 Symfony<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e Doctrine \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043e (Lazy load).\u00a0\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0441\u0432\u044f\u0437\u0435\u0439 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u044f\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430. \u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b Doctrine \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (fetch:&#8217;EAGER&#8217;), \u043e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443.<\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 Symfony, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 (\u043d\u0435)\u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u0415\u0441\u0442\u044c \u0432\u0438\u0442\u0440\u0438\u043d\u0430 \u0441 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<figure class=\"bordered\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3c6\/a38\/565\/3c6a38565f7898b35b4438725808ad77.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445\" width=\"363\" height=\"231\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3c6\/a38\/565\/3c6a38565f7898b35b4438725808ad77.png\"\/><\/p>\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430:<\/strong> \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u042d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u043e\u0440\u043c\u0430\u0442: \/books?with[]=author&amp;with[]=author_subscribers, \u0433\u0434\u0435 with &#8212; \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0439 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<h2>\u041f\u0435\u0440\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0435 \u043c\u0443\u0434\u0440\u0441\u0442\u0432\u0443\u044f \u043b\u0443\u043a\u0430\u0432\u043e, \u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434:<\/p>\n<pre><code class=\"php\">    #[Route('\/books', name: 'app_book_index')]     public function index(Request $request): Response     {         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c Request         $requestListBook = $this->serializer->deserialize(             json_encode($request->query->all()),             RequestListBook::class,             'json'         );         $requestListBook->validate();          \/\/ \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442         $books = $this->bookRepository->findAll();          $data = $this->serializer->serialize($books, 'json', [             'groups' => $requestListBook->getWith(),         ]);          return new JsonResponse($data, 200, [], true);     }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u044b \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 with.<\/p>\n<h2>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 <\/h2>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 (with=[author,author_subscribers]), \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c Lazy Load:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/11b\/545\/fac\/11b545face68ead64d7c014e35fb54e5.png\" alt=\"\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Lazy Load\" title=\"\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Lazy Load\" width=\"986\" height=\"735\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/11b\/545\/fac\/11b545face68ead64d7c014e35fb54e5.png\"\/><\/p>\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Lazy Load<\/figcaption><\/div>\n<\/figure>\n<p>Doctrine \u043c\u043e\u0436\u0435\u0442 \u0436\u0430\u0434\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c (Eager) \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (fetch: &#8216;EAGER&#8217;):<\/p>\n<pre><code class=\"php\">class Book     #[ORM\\ManyToOne(fetch: 'EAGER', inversedBy: 'books')]     #[ORM\\JoinColumn(nullable: false)]     #[Groups(['author', 'author_subscribers'])]     private ?Author $author = null;  class Author     #[ORM\\ManyToMany(targetEntity: Subscriber::class, mappedBy: 'authors', fetch: 'EAGER')]     #[Groups(['author_subscribers])]     private Collection $subscribers;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e68\/876\/5a2\/e688765a2acb2534bba983c1f240ba5e.png\" alt=\"\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Eager Load\" title=\"\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Eager Load\" width=\"986\" height=\"623\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e68\/876\/5a2\/e688765a2acb2534bba983c1f240ba5e.png\"\/><\/p>\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Eager Load<\/figcaption><\/div>\n<\/figure>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441, \u043a \u0442\u043e\u043c\u0443 \u0436\u0435, \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0435\u0449\u0451 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 books \u0431\u0435\u0437 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 (with=[]), \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9fc\/727\/ddd\/9fc727ddda59252cacfdd43e5147328d.png\" alt=\"\u041b\u0438\u0448\u043d\u0438\u0439 JOIN\" title=\"\u041b\u0438\u0448\u043d\u0438\u0439 JOIN\" width=\"986\" height=\"426\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9fc\/727\/ddd\/9fc727ddda59252cacfdd43e5147328d.png\"\/><\/p>\n<div><figcaption>\u041b\u0438\u0448\u043d\u0438\u0439 JOIN<\/figcaption><\/div>\n<\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0432\u0435\u0440\u043d\u0435\u043c Lazy load \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a Laravel.<\/p>\n<h2>\u0412\u0442\u043e\u0440\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 Laravel, \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f <code>with<\/code>. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"php\">class QueryBuilderService {     protected ?QueryBuilder $queryBuilder = null;      public function with($association, $alias, array $rootAliases = []): QueryBuilderService     {         $rootAliases = $rootAliases ?: $this->queryBuilder->getRootAliases();          if (count($rootAliases) === 1) {             \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0430\u043b\u0438\u0430\u0441, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0435\u0433\u043e             $rootAlias = reset($rootAliases);             $this->queryBuilder->leftJoin(\"$rootAlias.$association\", $alias)                 ->addSelect($alias);          } else {             \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445 \u0430\u043b\u0438\u0430\u0441\u043e\u0432, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u043b\u0438\u0430\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e             foreach ($rootAliases as $index => $rootAlias) {                 $this->queryBuilder->leftJoin(\"$rootAlias.$association\", \"$alias$index\")                     ->addSelect(\"$alias$index\");             }         }          return $this;     }      public function getQueryBuilder(): QueryBuilder     {         return $this->queryBuilder;     }      public function setQueryBuilder(QueryBuilder $queryBuilder): static     {         $this->queryBuilder = $queryBuilder;         return $this;     }  }<\/code><\/pre>\n<p>\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Symfony <a href=\"https:\/\/symfony.com\/doc\/current\/components\/workflow.html\" rel=\"noopener noreferrer nofollow\">workflow<\/a>, \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445. \u041d\u043e \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043d\u0435\u0447\u0442\u043e \u0441\u0445\u043e\u0436\u0435\u0435 \u0441 Laravel pipeline:<\/p>\n<pre><code class=\"php\">abstract class AbstractPipeline {     protected array $pipes;      protected array|string $passable;      public function through(array $pipes): static     {         foreach ($pipes as $pipe) {             $this->pipes[] = $pipe;         }          return $this;     }      public function send(array|string $passable): static     {         $this->passable = $passable;         return $this;     }          abstract public function handle(mixed $request): void; }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442:<\/p>\n<pre><code class=\"php\">interface BookListHandlerInterface {     public function handle(mixed $passable, mixed &amp;$qb): void; }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 author \u0438 author_subscribers:<\/p>\n<pre><code class=\"php\">class WithAuthorHandler implements BookListHandlerInterface {      public function handle(mixed $passable, mixed &amp;$qb): void     {         if (in_array(\"author\", $passable) || in_array(\"author_subscribers\", $passable)) {             $qb->with('author', 'a');         }     } }  class WithAuthorSubscribersHandler implements BookListHandlerInterface {     public function handle(mixed $passable, mixed &amp;$qb): void     {         if (in_array(\"author_subscribers\", $passable)) {             $qb->with('subscribers', 's', ['a']);         }     } }  <\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430 (\u0434\u043b\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0432\u043e\u0441\u043f\u0440\u0438\u044f\u0442\u0438\u044f \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435):<\/p>\n<pre><code class=\"php\">    #[Route('\/books', name: 'app_book')]     public function index(Request $request, BookListPipeline $pipeline): Response     {         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c Request         $requestListBook = $this->serializer->deserialize(             json_encode($request->query->all()),             RequestListBook::class,             'json'         );         $requestListBook->validate();          \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430         $qb = $this->bookRepository->createQueryBuilder('t');         $qbService = $this->queryBuilderService->setQueryBuilder($qb);          \/\/ \u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435         $pipeline             ->send($requestListBook->getWith())             ->through([                 new WithAuthorHandler(),                 new WithAuthorSubscribersHandler(),             ])             ->handle($qbService);          \/\/ \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442         $books = $qbService->getQueryBuilder()->getQuery()->getResult();          $data = $this->serializer->serialize($books, 'json', [             'groups' => $requestListBook->getWith(),         ]);          return new JsonResponse($data, 200, [], true);     }<\/code><\/pre>\n<h2>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h2>\n<p>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 (\u0432 Response \u0442\u043e\u043b\u044c\u043a\u043e book \u0441 id=1):<\/p>\n<p><strong>\/books?with[]=author&amp;with[]=author_subscribers<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>    {         \"id\": 1,         \"title\": \"book1\",         \"author\": {             \"id\": 1,             \"name\": \"Author1\",             \"subscribers\": [                 {                     \"id\": 1,                     \"name\": \"sub1\"                 },                 {                     \"id\": 3,                     \"name\": \"sub3\"                 },                 {                     \"id\": 4,                     \"name\": \"string\"                 },                 {                     \"id\": 5,                     \"name\": \"string1\"                 },                 {                     \"id\": 6,                     \"name\": \"string12\"                 },                 {                     \"id\": 7,                     \"name\": \"string123\"                 },                 {                     \"id\": 8,                     \"name\": \"string1243\"                 }             ]         }     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/001\/104\/385\/001104385754f61bfe4ecae547e12e5e.png\" alt=\"\" title=\"\" width=\"986\" height=\"479\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/001\/104\/385\/001104385754f61bfe4ecae547e12e5e.png\"\/><\/figure>\n<p><strong>\/books?with[]=author<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>    {         \"id\": 1,         \"title\": \"book1\",         \"author\": {             \"id\": 1,             \"name\": \"Author1\"         }     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/16a\/b4f\/2a8\/16ab4f2a8bdc43bf6f5b2145de8a06fe.png\" width=\"986\" height=\"479\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/16a\/b4f\/2a8\/16ab4f2a8bdc43bf6f5b2145de8a06fe.png\"\/><\/figure>\n<p><strong>\/books<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>   {         \"id\": 1,         \"title\": \"book1\"     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/313\/c8f\/997\/313c8f9975ea42dff6eba47b1ff772fe.png\" width=\"986\" height=\"423\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/313\/c8f\/997\/313c8f9975ea42dff6eba47b1ff772fe.png\"\/><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u044b\u043b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432\u0430\u0436\u043d\u044b\u0439 \u0430\u0441\u043f\u0435\u043a\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Doctrine \u0432 Symfony &#8212; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e Doctrine \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 (Lazy Load), \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<p>\u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u0435\u043a\u043e \u0434\u043e \u0438\u0434\u0435\u0430\u043b\u0430, \u043d\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>*\u041d\u0430 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0443.<\/p>\n<p>\u0427\u0442\u043e, \u0435\u0441\u043b\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432 api \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u043b\u044e\u0447\u0435 included? \u0412 Laravel \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c API <a href=\"https:\/\/laravel.com\/docs\/10.x\/eloquent-resources#resource-collections\" rel=\"noopener noreferrer nofollow\">Resouces<\/a>. <\/p>\n<p>\u0423 Spatie \u0435\u0441\u0442\u044c <a href=\"https:\/\/spatie.be\/docs\/laravel-query-builder\/v5\/introduction\" rel=\"noopener noreferrer nofollow\">QueryBuilder<\/a>, \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u0432 Symfony \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043b\u0430\u0441\u0441\u044b. \u0412\u0441\u0435\u043c \u0434\u043e\u0431\u0440\u0430!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/763084\/\"> https:\/\/habr.com\/ru\/articles\/763084\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e Doctrine \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043e (Lazy load).\u00a0\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0441\u0432\u044f\u0437\u0435\u0439 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u044f\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430. \u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b Doctrine \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (fetch:&#8217;EAGER&#8217;), \u043e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443.<\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 Symfony, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 (\u043d\u0435)\u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u0415\u0441\u0442\u044c \u0432\u0438\u0442\u0440\u0438\u043d\u0430 \u0441 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<figure class=\"bordered\">\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430:<\/strong> \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u042d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u043e\u0440\u043c\u0430\u0442: \/books?with[]=author&amp;with[]=author_subscribers, \u0433\u0434\u0435 with &#8212; \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0439 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<h2>\u041f\u0435\u0440\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0435 \u043c\u0443\u0434\u0440\u0441\u0442\u0432\u0443\u044f \u043b\u0443\u043a\u0430\u0432\u043e, \u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434:<\/p>\n<pre><code class=\"php\">    #[Route('\/books', name: 'app_book_index')]     public function index(Request $request): Response     {         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c Request         $requestListBook = $this->serializer->deserialize(             json_encode($request->query->all()),             RequestListBook::class,             'json'         );         $requestListBook->validate();          \/\/ \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442         $books = $this->bookRepository->findAll();          $data = $this->serializer->serialize($books, 'json', [             'groups' => $requestListBook->getWith(),         ]);          return new JsonResponse($data, 200, [], true);     }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u044b \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 with.<\/p>\n<h2>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 <\/h2>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 (with=[author,author_subscribers]), \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c Lazy Load:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Lazy Load<\/figcaption><\/div>\n<\/figure>\n<p>Doctrine \u043c\u043e\u0436\u0435\u0442 \u0436\u0430\u0434\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c (Eager) \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (fetch: &#8216;EAGER&#8217;):<\/p>\n<pre><code class=\"php\">class Book     #[ORM\\ManyToOne(fetch: 'EAGER', inversedBy: 'books')]     #[ORM\\JoinColumn(nullable: false)]     #[Groups(['author', 'author_subscribers'])]     private ?Author $author = null;  class Author     #[ORM\\ManyToMany(targetEntity: Subscriber::class, mappedBy: 'authors', fetch: 'EAGER')]     #[Groups(['author_subscribers])]     private Collection $subscribers;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438 Eager Load<\/figcaption><\/div>\n<\/figure>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441, \u043a \u0442\u043e\u043c\u0443 \u0436\u0435, \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0435\u0449\u0451 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 books \u0431\u0435\u0437 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 (with=[]), \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041b\u0438\u0448\u043d\u0438\u0439 JOIN<\/figcaption><\/div>\n<\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0432\u0435\u0440\u043d\u0435\u043c Lazy load \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a Laravel.<\/p>\n<h2>\u0412\u0442\u043e\u0440\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 Laravel, \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f <code>with<\/code>. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"php\">class QueryBuilderService {     protected ?QueryBuilder $queryBuilder = null;      public function with($association, $alias, array $rootAliases = []): QueryBuilderService     {         $rootAliases = $rootAliases ?: $this->queryBuilder->getRootAliases();          if (count($rootAliases) === 1) {             \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0430\u043b\u0438\u0430\u0441, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0435\u0433\u043e             $rootAlias = reset($rootAliases);             $this->queryBuilder->leftJoin(\"$rootAlias.$association\", $alias)                 ->addSelect($alias);          } else {             \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445 \u0430\u043b\u0438\u0430\u0441\u043e\u0432, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u043b\u0438\u0430\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e             foreach ($rootAliases as $index => $rootAlias) {                 $this->queryBuilder->leftJoin(\"$rootAlias.$association\", \"$alias$index\")                     ->addSelect(\"$alias$index\");             }         }          return $this;     }      public function getQueryBuilder(): QueryBuilder     {         return $this->queryBuilder;     }      public function setQueryBuilder(QueryBuilder $queryBuilder): static     {         $this->queryBuilder = $queryBuilder;         return $this;     }  }<\/code><\/pre>\n<p>\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Symfony <a href=\"https:\/\/symfony.com\/doc\/current\/components\/workflow.html\" rel=\"noopener noreferrer nofollow\">workflow<\/a>, \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445. \u041d\u043e \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043d\u0435\u0447\u0442\u043e \u0441\u0445\u043e\u0436\u0435\u0435 \u0441 Laravel pipeline:<\/p>\n<pre><code class=\"php\">abstract class AbstractPipeline {     protected array $pipes;      protected array|string $passable;      public function through(array $pipes): static     {         foreach ($pipes as $pipe) {             $this->pipes[] = $pipe;         }          return $this;     }      public function send(array|string $passable): static     {         $this->passable = $passable;         return $this;     }          abstract public function handle(mixed $request): void; }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442:<\/p>\n<pre><code class=\"php\">interface BookListHandlerInterface {     public function handle(mixed $passable, mixed &amp;$qb): void; }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 author \u0438 author_subscribers:<\/p>\n<pre><code class=\"php\">class WithAuthorHandler implements BookListHandlerInterface {      public function handle(mixed $passable, mixed &amp;$qb): void     {         if (in_array(\"author\", $passable) || in_array(\"author_subscribers\", $passable)) {             $qb->with('author', 'a');         }     } }  class WithAuthorSubscribersHandler implements BookListHandlerInterface {     public function handle(mixed $passable, mixed &amp;$qb): void     {         if (in_array(\"author_subscribers\", $passable)) {             $qb->with('subscribers', 's', ['a']);         }     } }  <\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430 (\u0434\u043b\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0432\u043e\u0441\u043f\u0440\u0438\u044f\u0442\u0438\u044f \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435):<\/p>\n<pre><code class=\"php\">    #[Route('\/books', name: 'app_book')]     public function index(Request $request, BookListPipeline $pipeline): Response     {         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c Request         $requestListBook = $this->serializer->deserialize(             json_encode($request->query->all()),             RequestListBook::class,             'json'         );         $requestListBook->validate();          \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430         $qb = $this->bookRepository->createQueryBuilder('t');         $qbService = $this->queryBuilderService->setQueryBuilder($qb);          \/\/ \u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435         $pipeline             ->send($requestListBook->getWith())             ->through([                 new WithAuthorHandler(),                 new WithAuthorSubscribersHandler(),             ])             ->handle($qbService);          \/\/ \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442         $books = $qbService->getQueryBuilder()->getQuery()->getResult();          $data = $this->serializer->serialize($books, 'json', [             'groups' => $requestListBook->getWith(),         ]);          return new JsonResponse($data, 200, [], true);     }<\/code><\/pre>\n<h2>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h2>\n<p>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 (\u0432 Response \u0442\u043e\u043b\u044c\u043a\u043e book \u0441 id=1):<\/p>\n<p><strong>\/books?with[]=author&amp;with[]=author_subscribers<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>    {         \"id\": 1,         \"title\": \"book1\",         \"author\": {             \"id\": 1,             \"name\": \"Author1\",             \"subscribers\": [                 {                     \"id\": 1,                     \"name\": \"sub1\"                 },                 {                     \"id\": 3,                     \"name\": \"sub3\"                 },                 {                     \"id\": 4,                     \"name\": \"string\"                 },                 {                     \"id\": 5,                     \"name\": \"string1\"                 },                 {                     \"id\": 6,                     \"name\": \"string12\"                 },                 {                     \"id\": 7,                     \"name\": \"string123\"                 },                 {                     \"id\": 8,                     \"name\": \"string1243\"                 }             ]         }     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p><strong>\/books?with[]=author<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>    {         \"id\": 1,         \"title\": \"book1\",         \"author\": {             \"id\": 1,             \"name\": \"Author1\"         }     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p><strong>\/books<\/strong><\/p>\n<details class=\"spoiler\">\n<summary>Response<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>   {         \"id\": 1,         \"title\": \"book1\"     },<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u044b\u043b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432\u0430\u0436\u043d\u044b\u0439 \u0430\u0441\u043f\u0435\u043a\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Doctrine \u0432 Symfony &#8212; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e Doctrine \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 (Lazy Load), \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<p>\u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u0435\u043a\u043e \u0434\u043e \u0438\u0434\u0435\u0430\u043b\u0430, \u043d\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>*\u041d\u0430 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0443.<\/p>\n<p>\u0427\u0442\u043e, \u0435\u0441\u043b\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432 api \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u043b\u044e\u0447\u0435 included? \u0412 Laravel \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c API <a href=\"https:\/\/laravel.com\/docs\/10.x\/eloquent-resources#resource-collections\" rel=\"noopener noreferrer nofollow\">Resouces<\/a>. <\/p>\n<p>\u0423 Spatie \u0435\u0441\u0442\u044c <a href=\"https:\/\/spatie.be\/docs\/laravel-query-builder\/v5\/introduction\" rel=\"noopener noreferrer nofollow\">QueryBuilder<\/a>, \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u0432 Symfony \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043b\u0430\u0441\u0441\u044b. \u0412\u0441\u0435\u043c \u0434\u043e\u0431\u0440\u0430!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/763084\/\"> https:\/\/habr.com\/ru\/articles\/763084\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-355714","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/355714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355714"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/355714\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}