Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
循環参照してるEntityのシリアライズとかの話
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Takashi Kanemoto
July 08, 2018
810
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
循環参照してるEntityのシリアライズとかの話
Symfony Meetup #21 LT 資料
https://symfony.connpass.com/event/90319/
Takashi Kanemoto
July 08, 2018
More Decks by Takashi Kanemoto
See All by Takashi Kanemoto
API Platformを活用したPHPによる本格的なWeb API開発 / api-platform-book-intro
ttskch
2
320
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
5
1.6k
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
610
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
1.4k
今年書いた技術記事で伸びたやつの自慢と自分の中では力作なのにさっぱり伸びなかったやつの供養 / My Tech Articles 2024
ttskch
2
240
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
48
45k
データベース/SQL超入門!完全初心者向けに世界一分かりやすく解説します
ttskch
2
8.7k
Symfony UX Autocompleteとかいう 顧客が本当に必要だったもの
ttskch
0
2.4k
就活生あるいは新人エンジニアさんへのお節介なアドバイス
ttskch
0
1.7k
Featured
See All Featured
Designing for Performance
lara
611
70k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
460
4 Signs Your Business is Dying
shpigford
187
23k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Making Projects Easy
brettharned
120
6.8k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
450
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
For a Future-Friendly Web
brad_frost
183
10k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
330
Transcript
/30 1 ॥ࢀরͯ͠ΔEntityͷ γϦΞϥΠζͱ͔ͷ #symfony_meetup 2018.07.08 @ttskch
ttskchʢ͖ͨͭͪʣ 2 • ໊ݹ͔Βདྷ·ͨ͠ • (ג)ΧϧςοτίϛϡχέʔγϣϯζCTO • PHPྺ6.5ɺSymfonyྺ4ʢROMઐʣ • ͓ख͍ͨ͠ຊʢ2015/12ʣ
/30
͋͞ɺ͋ͳͨࠓ͙͢ϑΥϩʔ͠Α͏!!! શແྉ ͨ ͭ ͖ ͪ twitter.com/ttskch ·͞ʹ໊ݴػ ਓੜͰେͳ͜ͱɺͯ͢ ͖͔ͨͭͪΒڭΘͬͨ
͖ͨͭͪΛϑΥϩʔͨ͠Β ࠊ௧͕࣏Γ·ͨ͠ ͍ͯ͠Δ ͍ͯ͠ͳ͍ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋ 3/30
• PHPer/JSer ઈࢍେืूதʂʢϑϧϦϞʔτϫʔΫՄʂʣ • ϦεςΟϯάࠂӡ༻πʔϧʮLisketʯΛఏڙ • ࣌ΛઌऔΔϗϫΠτاۀͰ͢ (ג)Χϧςοτίϛϡχέʔγϣϯζ 4 ಛผड
/30
5 (ג)Χϧςοτίϛϡχέʔγϣϯζ ΤϯδχΞͷಇ͖͢͞େࣄʹͯ͠·͢ /30
6 (ג)Χϧςοτίϛϡχέʔγϣϯζ SNSͳͲ͔Β͓ؾܰʹ͝࿈བྷ͍ͩ͘͞✨ /30
/30 7 ॥ࢀরͯ͠ΔEntityͷ γϦΞϥΠζͱ͔ͷ #symfony_meetup 2018.07.08 @ttskch /30
class User { public $name; /** * @var Post[] */
public $posts; } 8 Α͋͘Δ User(1) - Post(N) class Post { public $title; /** * @var User */ public $user; } /30
$serializer = SerializerBuilder::create()->build(); $json = $serializer->serialize($user, 'json'); echo $json; 9
jms/serializerͰUserΛγϦΞϥΠζ /30
{ "name": "user1", "posts": [ { "title": "post1" }, {
"title": "post2" } ] } 10 ݁Ռ posts.user͕ফ͑ڈͬͯΔ /30
class User { public $name; /** * @var Post[] */
public $posts; } 11 user.posts.user = userʢ॥ࢀরʣ class Post { public $title; /** * @var User */ public $user; } /30
ͳΔ΄Ͳ͔֬ʹ 12 • ͩͬͨΒɺMaxDepth(1) ͱ͔͓͚ͬͯ ҆શʹγϦΞϥΠζͰ͖ΔͷͰʁ /30
use JMS\Serializer\Annotation\MaxDepth; class User { public $name; /** * @var
Post[] * * @MaxDepth(1) */ public $posts; } 13 ͬͯΈͨ /30
- $serializer = SerializerBuilder::create()->build(); + $serializer = SerializerBuilder::create() + ->setSerializationContextFactory(function
() { + return SerializationContext::create() + ->enableMaxDepthChecks() + ; + }) + ->build() + ; 14 ͬͯΈͨ MaxDepthͷνΣοΫΛ༗ޮԽ /30
{ "name": "user1", "posts": [ { "title": "post1" }, {
"title": "post2" } ] } 15 ݁Ռ มΘΒͣɻ /30
ΑΖ͍͠, ͳΒ print σόοάͩ 16/30
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
$serializer = SerializerBuilder::create() ->setSerializationContextFactory(function () { return SerializationContext::create() ->enableMaxDepthChecks() +
->setSerializeNull(true) ; }) ->build() ; 18 nullγϦΞϥΠζ͢ΔΑ͏ʹઃఆ /30
{ "name": "user1", "posts": [ { "title": "post1", "user": null
}, { "title": "post2", "user": null } ] } 19 ֬ೝ post.user͔֬ʹnullʹͳͬͯΔ /30
͞Βʹ print σόοά 20/30
if ($context->isVisiting($data)) { return null; } 21 GraphNavigator.php ͜͜Ͱnullʹͳͬͯͨ /30
if ($context->isVisiting($data)) { return null; } 22 ࣮ݧɿಉҰΦϒδΣΫτ͡Όͳ͚Ε͍͚Δͷ͔ ϓϩύςΟΛτοϓμϯͰḷΓͭͭɺ๚த ͷΦϒδΣΫτΛελοΫ͓͍ͯͯ͠ɺ৽ͨʹ๚
ͨ͠ΦϒδΣΫτ͕๚தελοΫʹੵ·Ε ͯͨΒ࠶ؼͳͷͰnullʹ͢Δɺͱ͍͏࣮Έ͍ͨ /30
foreach ($user->posts as $post) { $post->user = clone $user; }
$json = $serializer->serialize($user, 'json'); 23 clone $user ͯ͠ஔ͖͑ͯΈΔ /30
{ "name": "user1", "posts": [ { "title": "post1", "user": {
"name": "user1", "posts": [ null, null ] } }, : ] } 24 ݁Ռ ͳΔ΄Ͳߦ͚ͨɻ /30
݁ 25 • jms/serializerͰɺಉҰΦϒδΣΫτ͕࠶ؼ తʹࢀর͞ΕΔΤϯςΟςΟγϦΞϥΠζ Ͱ͖ͳ͍ • MaxDepthͷνΣοΫҎલʹ͔ΕͯΔʢଟʣ /30
͡Ό͋ Ͳ͏͢Εʁ 26/30
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
// 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 • jms/serializerͰ॥ࢀর͍ͯ͠ΔΤϯςΟ ςΟγϦΞϥΠζͰ͖ͳ͍ • ͜͏͍͏߹APIΛ͚Δͷ͕Ұൠతʢଟʣ • Qiitaʹࡉ͔͘ॻ͍ͯ·͢ https://bit.ly/2uaINck
/30
Thanks :) 30/30