Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PofEAAで読み解くDoctrine2

男爵
June 16, 2018

 PofEAAで読み解くDoctrine2

PHPカンファレンス福岡2018の15分枠の発表資料です。

2018.06.19 口頭で説明した内容を追記

男爵

June 16, 2018
Tweet

More Decks by 男爵

Other Decks in Programming

Transcript

  1. 自己紹介 ⊡ 本名:丹賀健一 ⊡ 通称:男爵 ⊡ dnskimo ⊡ dnskimox ⊡

    北海道在住 ⊡ 株式会社インフィニットループ ⊡ ソシャゲバックエンドAPI開発
  2. P of EAA https://ja.wikipedia.org/wiki/マーティン・ファウ ラー ⊡ 原題『Patterns of Enterprise Application

    Architecture』 ⊡ マーティン・ファウラー著 ⊡ エンタープライズアプリケーションの ための設計パターン集
  3. Catalog of P of EAA ⊡ Domain Model ⊡ Active

    Record ⊡ Data Mapper ⊡ Unit of Work ⊡ Identity Map ⊡ Metadata Mapping ⊡ Foreign Key Mapping ⊡ Lazy Load ⊡ Single Table Inheritance ⊡ Repository ⊡ Model View Controller ⊡ Front Controller ⊡ Remote Facade ⊡ Service Layer ⊡ Data Transfer Object ⊡ Optimistic Offline Lock ⊡ Layer Supertype ⊡ Separated Interface ⊡ Value Object ⊡ etc...
  4. “ A layer of Mappers that moves data between objects

    and a database while keeping them independent of each other and the mapper itself.
  5. CRUDのR $em = EntityManager::create(...); $characterRepository = $em->getRepository(Character::class); $character = $characterRepository->find(1);

    // Executed SQL: SELECT FROM characters WHERE id = 1; $characters = $characterRepository->findByName(“Bob”); // Executed SQL: SELECT FROM characters WHERE name = ‘Bob’; $characters = $characterRepository->findAll(); // Executed SQL: SELECT FROM characters;
  6. 経験値獲得メソッド class Character... public function gainExp(int $exp) { assert(0 <

    $exp); $this->setExp($this->getExp() + $exp); // 以下レベルアップ判定など ... } }
  7. CRUDのU $em = EntityManager::create(...); $characterRepository = $em->getRepository(Character::class); $character = $characterRepository->find(1);

    // Executed SQL: SELECT FROM characters WHERE id = 1; $character->gainExp(100); $em->flush(); // Executed SQL: UPDATE characters SET exp = 100 WHERE id = 1;
  8. “ Maintains a list of objects affected by a business

    transaction and coordinates the writing out of changes and the resolution of concurrency problems.
  9. ロストアップデートの予感…… $character1A = $characterRepository->find(1); // Executed SQL: SELECT FROM characters

    WHERE id = 1; $character1B = $characterRepository->find(1); // Executed SQL?: SELECT FROM characters WHERE id = 1; $character1A->gainExp(100); $character1B->gainExp(100); $em->flush(); // Executed SQL?: UPDATE characters SET exp = 100 WHERE id = 1; // Executed SQL?: UPDATE characters SET exp = 100 WHERE id = 1;
  10. “ Ensures that each object gets loaded only once by

    keeping every loaded object in a map. Looks up objects using the map when referring to them.
  11. ロストアップデートは起きない $character1A = $characterRepository->find(1); // Executed SQL: SELECT FROM characters

    WHERE id = 1; $character1B = $characterRepository->find(1); // Executed SQL: NONE $character1A->gainExp(100); $character1B->gainExp(100); $em->flush(); // Executed SQL: UPDATE characters SET exp = 200 WHERE id = 1;
  12. その他Doctrine2で使われているパ ターン ⊡ Domain Model ⊡ Active Record ⊡ Data

    Mapper ⊡ Unit of Work ⊡ Identity Map ⊡ Metadata Mapping ⊡ Foreign Key Mapping ⊡ Lazy Load ⊡ Single Table Inheritance ⊡ Repository ⊡ Model View Controller ⊡ Front Controller ⊡ Remote Facade ⊡ Service Layer ⊡ Data Transfer Object ⊡ Optimistic Offline Lock ⊡ Layer Supertype ⊡ Separated Interface ⊡ Value Object ⊡ etc...
  13. 参考文献 ⊡ Patterns of Enterprise Application Architecture ⊡ Catalog of

    Patterns of Enterprise Application Architecture ⊡ Data Mapper ⊡ Unit of Work ⊡ Identity Map ⊡ POJO