{"action":"opened","issue":{"url":"https://api.github.com/repos/Ocramius/GeneratedHydrator/issues/65","repository_url":"https://api.github.com/repos/Ocramius/GeneratedHydrator","labels_url":"https://api.github.com/repos/Ocramius/GeneratedHydrator/issues/65/labels{/name}","comments_url":"https://api.github.com/repos/Ocramius/GeneratedHydrator/issues/65/comments","events_url":"https://api.github.com/repos/Ocramius/GeneratedHydrator/issues/65/events","html_url":"https://github.com/Ocramius/GeneratedHydrator/issues/65","id":218775026,"number":65,"title":"Doctrine Hydrator implementation?","user":{"login":"podorozhny","id":5286162,"avatar_url":"https://avatars1.githubusercontent.com/u/5286162?v=3","gravatar_id":"","url":"https://api.github.com/users/podorozhny","html_url":"https://github.com/podorozhny","followers_url":"https://api.github.com/users/podorozhny/followers","following_url":"https://api.github.com/users/podorozhny/following{/other_user}","gists_url":"https://api.github.com/users/podorozhny/gists{/gist_id}","starred_url":"https://api.github.com/users/podorozhny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/podorozhny/subscriptions","organizations_url":"https://api.github.com/users/podorozhny/orgs","repos_url":"https://api.github.com/users/podorozhny/repos","events_url":"https://api.github.com/users/podorozhny/events{/privacy}","received_events_url":"https://api.github.com/users/podorozhny/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-04-02T16:21:11Z","updated_at":"2017-04-02T16:21:11Z","closed_at":null,"body":"Hello, @Ocramius.\r\n\r\nI'm wondering what you think about implementing Doctrine GeneratedHydrator. I mean something like `GeneratedHydrator extends \\Doctrine\\ORM\\Internal\\Hydration\\AbstractHydrator`.\r\n\r\nWhat do you think, is this a good idea? Will it have any advantages over ObjectHydrator?\r\n\r\nOr maybe someone has already implemented it? :)\r\n\r\nMy dream is to learn how to fetch & hydrate & serialize large collections of entities with doctrine for an acceptable time.\r\n\r\nFor example I have these entities:\r\n\r\n``` php\r\nclass Post {\r\n    /** @ORM\\Id() */\r\n    private $id;\r\n\r\n    /** @ORM\\Column(type=\"string\") */\r\n    private $name;\r\n\r\n    /** @ORM\\OneToMany(targetEntity=\"PostTag\") */\r\n    private $postTags;\r\n\r\n    public function getTags(): Tag[]\r\n    {\r\n        return array_map(\r\n            function (PostTag $postTag) {\r\n                return $postTag->getTag();\r\n            },\r\n            $this->postTags->toArray()\r\n        )\r\n    }\r\n}\r\n\r\nclass PostTag {\r\n    /** @ORM\\ManyToOne(targetEntity=\"Post\") */\r\n    private $post;\r\n\r\n    /** @ORM\\ManyToOne(targetEntity=\"Tag\") */\r\n    private $tag;\r\n}\r\n\r\nclass Tag {\r\n    /** @ORM\\Id() */\r\n    private $id;\r\n\r\n    /** @ORM\\Column(type=\"string\") */\r\n    private $name;\r\n\r\n    /** @ORM\\OneToMany(targetEntity=\"PostTag\") */\r\n    private $postTags;\r\n\r\n    ...\r\n\r\n    public function getNameProcessedWithSomeKindOfMagic(): string\r\n    {\r\n        return processWithSomeKindOfMagic($this->name);\r\n    }\r\n}\r\n```\r\n\r\nIn my JSON API REST controller I want to do something like that:\r\n``` php\r\nclass PostController {\r\n    public function getAllAction(): Response\r\n    {\r\n        $queryBuilder = $this->postRepository->createQueryBuilder('post')\r\n            ->orderBy('post.id');\r\n\r\n        /** @var Post[] $posts */\r\n        $posts = $queryBuilder->getQuery()->getResult();\r\n\r\n        // Thx for 2-step hydration trick :)\r\n        $this->postRepository->createQueryBuilder('post')\r\n            ->select('PARTIAL post.{id}')\r\n            ->addSelect('postTag')\r\n            ->addSelect('tag')\r\n            ->leftJoin('post.postTags', 'postTag')\r\n            ->leftJoin('postTag.tag', 'tag')\r\n            ->getQuery()\r\n            ->getResult();\r\n\r\n        // In this place I use Symfony Serializer with serialization mappings for each entity\r\n        // Normalized post will look like that example:\r\n        // $post = [\r\n        //     'id' => 1,\r\n        //     'name' => 'Foo Bar',\r\n        //     'tags' => [\r\n        //         [\r\n        //             'id' => 1,\r\n        //             'name' => 'foo',\r\n        //             'name_processed_with_some_kind_of_magic' => 'magic_foo'\r\n        //         ],\r\n        //         [\r\n        //             'id' => 2,\r\n        //             'name' => 'bar',\r\n        //             'name_processed_with_some_kind_of_magic' => 'magic_bar'\r\n        //         ]\r\n        //     ],\r\n        // ];\r\n        $data = $this->serializer->serialize($posts);\r\n\r\n        return new JsonResponse($data, 200, [], true);\r\n    }\r\n}\r\n```\r\n\r\nWith few thousands posts in database this action will take SECONDS to generate JSON from database data and the only reason here will be complicated doctrine object hydration.\r\nSwitching to ArrayHydrator will reduce it to a couple of hundreds milliseconds, but I can't use it because entities is not a simple DTOs (look at `getTags` and `getNameProcessedWithSomeKindOfMagic` method examples). Database data denormalization and ArrayHydrator usage is also a pretty good solution in _big_ collections API, but I wonder if its not the only solution.  \r\n\r\nMy apologies for the fact that this longread is not directly related to your library, this is a really big problem for me and I will be infinitely glad for your ideas on this issue.\r\n\r\nThanks!\r\n"}}
{"actor":{"display_login":"podorozhny"}}