循環参照してるEntityのシリアライズとかの話

8e844e43f029ec38e206a464cf4587b3?s=47 Takashi Kanemoto
July 08, 2018
270

 循環参照してるEntityのシリアライズとかの話

Symfony Meetup #21 LT 資料
https://symfony.connpass.com/event/90319/

8e844e43f029ec38e206a464cf4587b3?s=128

Takashi Kanemoto

July 08, 2018
Tweet

Transcript

  1. /30 1 ॥؀ࢀরͯ͠ΔEntityͷ γϦΞϥΠζͱ͔ͷ࿩ #symfony_meetup 2018.07.08 @ttskch

  2. ttskchʢ͖ͨͭͪʣ 2 • ໊ݹ԰͔Βདྷ·ͨ͠ • (ג)ΧϧςοτίϛϡχέʔγϣϯζCTO • PHPྺ6.5೥ɺSymfonyྺ4೥ʢROMઐʣ • ͓ख఻͍ͨ͠ຊʢ2015/12ʣ

    /30
  3. ͋͞ɺ͋ͳͨ΋ࠓ͙͢ϑΥϩʔ͠Α͏!!! ׬શແྉ ͨ ͭ ͖ ͪ twitter.com/ttskch ·͞ʹ໊ݴ੡଄ػ ਓੜͰେ੾ͳ͜ͱ͸ɺ͢΂ͯ ͖͔ͨͭͪΒڭΘͬͨ

    ͖ͨͭͪΛϑΥϩʔͨ͠Β
 ࠊ௧͕࣏Γ·ͨ͠  ͍ͯ͠Δ ͍ͯ͠ͳ͍ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋཰ 3/30
  4. • PHPer/JSer ઈࢍେืूதʂʢϑϧϦϞʔτϫʔΫՄʂʣ • ϦεςΟϯά޿ࠂӡ༻πʔϧʮLisketʯΛఏڙ • ࣌୅ΛઌऔΔϗϫΠτاۀͰ͢ (ג)Χϧςοτίϛϡχέʔγϣϯζ 4 ಛผ৆ड৆

    /30
  5. 5 (ג)Χϧςοτίϛϡχέʔγϣϯζ ΤϯδχΞͷಇ͖΍͢͞௒େࣄʹͯ͠·͢ /30

  6. 6 (ג)Χϧςοτίϛϡχέʔγϣϯζ SNSͳͲ͔Β͓ؾܰʹ͝࿈བྷ͍ͩ͘͞✨ /30

  7. /30 7 ॥؀ࢀরͯ͠ΔEntityͷ γϦΞϥΠζͱ͔ͷ࿩ #symfony_meetup 2018.07.08 @ttskch /30

  8. class User { public $name; /** * @var Post[] */

    public $posts; } 8 Α͋͘Δ User(1) - Post(N) class Post { public $title; /** * @var User */ public $user; } /30
  9. $serializer = SerializerBuilder::create()->build(); $json = $serializer->serialize($user, 'json'); echo $json; 9

    jms/serializerͰUserΛγϦΞϥΠζ /30
  10. { "name": "user1", "posts": [ { "title": "post1" }, {

    "title": "post2" } ] } 10 ݁Ռ posts.user͕ফ͑ڈͬͯΔ /30
  11. class User { public $name; /** * @var Post[] */

    public $posts; } 11 user.posts.user = userʢ॥؀ࢀরʣ class Post { public $title; /** * @var User */ public $user; } /30
  12. ͳΔ΄Ͳ͔֬ʹ 12 • ͩͬͨΒɺMaxDepth(1) ͱ͔΍͓͚ͬͯ͹
 ҆શʹγϦΞϥΠζͰ͖ΔͷͰ͸ʁ /30

  13. use JMS\Serializer\Annotation\MaxDepth; class User { public $name; /** * @var

    Post[] * * @MaxDepth(1) */ public $posts; } 13 ΍ͬͯΈͨ /30
  14. - $serializer = SerializerBuilder::create()->build(); + $serializer = SerializerBuilder::create() + ->setSerializationContextFactory(function

    () { + return SerializationContext::create() + ->enableMaxDepthChecks() + ; + }) + ->build() + ; 14 ΍ͬͯΈͨ MaxDepthͷνΣοΫΛ༗ޮԽ /30
  15. { "name": "user1", "posts": [ { "title": "post1" }, {

    "title": "post2" } ] } 15 ݁Ռ มΘΒͣɻ /30
  16. ΑΖ͍͠, ͳΒ͹ print σόοάͩ 16/30

  17. if ((null === $v && $context->shouldSerializeNull() !== true) || (true

    === $metadata->skipWhenEmpty && ($v instanceof \ArrayObject || \is_array($v)) && 0 === count($v)) ) { return; } 17 JsonSerializationVisitor.php ͜͜Ͱreturnͯͨ͠ʢ஋͕null͔ͭɺnull͸γϦΞϥΠζ͠ͳ͍ઃఆʣ /30
  18. $serializer = SerializerBuilder::create() ->setSerializationContextFactory(function () { return SerializationContext::create() ->enableMaxDepthChecks() +

    ->setSerializeNull(true) ; }) ->build() ; 18 null΋γϦΞϥΠζ͢ΔΑ͏ʹઃఆ /30
  19. { "name": "user1", "posts": [ { "title": "post1", "user": null

    }, { "title": "post2", "user": null } ] } 19 ֬ೝ post.user͸͔֬ʹnullʹͳͬͯΔ /30
  20. ͞Βʹ print σόοά 20/30

  21. if ($context->isVisiting($data)) { return null; } 21 GraphNavigator.php ͜͜Ͱnullʹͳͬͯͨ /30

  22. if ($context->isVisiting($data)) { return null; } 22 ࣮ݧɿಉҰΦϒδΣΫτ͡Όͳ͚Ε͹͍͚Δͷ͔ ϓϩύςΟΛτοϓμ΢ϯͰḷΓͭͭɺ๚໰த ͷΦϒδΣΫτΛελοΫ͓͍ͯͯ͠ɺ৽ͨʹ๚

    ໰ͨ͠ΦϒδΣΫτ͕๚໰தελοΫʹੵ·Ε ͯͨΒ࠶ؼͳͷͰnullʹ͢Δɺͱ͍͏࣮૷Έ͍ͨ /30
  23. foreach ($user->posts as $post) { $post->user = clone $user; }

    $json = $serializer->serialize($user, 'json'); 23 clone $user ͯ͠ஔ͖׵͑ͯΈΔ /30
  24. { "name": "user1", "posts": [ { "title": "post1", "user": {

    "name": "user1", "posts": [ null, null ] } }, : ] } 24 ݁Ռ ͳΔ΄Ͳߦ͚ͨɻ /30
  25. ݁࿦ 25 • jms/serializerͰ͸ɺಉҰΦϒδΣΫτ͕࠶ؼ తʹࢀর͞ΕΔΤϯςΟςΟ͸γϦΞϥΠζ Ͱ͖ͳ͍ • MaxDepthͷνΣοΫҎલʹ஄͔ΕͯΔʢଟ෼ʣ /30

  26. ͡Ό͋ Ͳ͏͢Ε͹ʁ 26/30

  27. APIΛ෼͚Δ 27 • userͱposts͸ผʑͷAPI͔Βऔಘ͢Δ • ϑϩϯτͷΠϝʔδ http.get(`/users/${user.id}`) .then(user => http.get(`/users/${user.id}/posts`)

    .then(posts => ({user, posts}))) .then(({user, posts}) => { user.posts = posts; this.user = user; }) ; /30
  28. // user-repository.ts get(id: string): Observable<User> { return this.http.get(`/users/${id}`).pipe( map(response =>

    getUserFromResponse(response)), switchMap((user: User) => this.postRepository.cgetByUser(user).pipe( map((posts: Post[]) => { user.posts = posts; return user; }) )) ); } 28 Angular (rxjs@6) ͩͱ͜Μͳײ͡ /30
  29. ·ͱΊ 29 • jms/serializerͰ͸॥؀ࢀর͍ͯ͠ΔΤϯςΟ ςΟ͸γϦΞϥΠζͰ͖ͳ͍ • ͜͏͍͏৔߹͸APIΛ෼͚Δͷ͕Ұൠతʢଟ෼ʣ • Qiitaʹࡉ͔͘ॻ͍ͯ·͢
 https://bit.ly/2uaINck

    /30
  30. Thanks :) 30/30