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

ORMユーザー対談 PHPカンファレンス関西2016

ORMユーザー対談 PHPカンファレンス関西2016

PHPカンファレンス関西2016 パネルディスカッション ORMユーザー対談 で使ったスライドです

8c1ecc738eee0254a41c2d4b33cda7b6?s=128

Hisateru Tanaka

July 16, 2016
Tweet

More Decks by Hisateru Tanaka

Other Decks in Technology

Transcript

  1. 03.Ϣʔβʔରஊ 1)1ΧϯϑΝϨϯεؔ੢

  2. ాதͻͯ͞Δ ϞσϨʔλʔ

  3. 8IZ w 1)1ʹ͸͍Ζ͍Ζͳ03.͕͋Δ w ςʔϒϧ͕ͭͳΒࠩ͸͋·Γͳ͍ w ςʔϒϧҎ্͋Δͱ͖ɺͦΕͧΕͷҧ͍Λ஌Γͨ ͍

  4. 4&-&$5 '30.CPPLT8)&3&JE $stmt = $pdo->query( "SELECT * FROM books WHERE

    id=1;” ); $book = $stmt->fetch(); echo $book['title'] . "\n";
  5. &MPRVFOU03. w -BSBWFMຊͷ%#ͷষΛ୲౰ w &MPRVFOU-BSBWFMͰ࢖ΘΕΔ03. w .PEFMΛܧঝ͢Δ͚ͩͰ࢖͑Δ"DUJWF3FDPSE େଜ૑ଠ࿠

  6. $book = Book::find(1); echo $book->title; use Illuminate\Database\Eloquent\Model; class Book extends

    Model { }
  7. %PDUSJOF03. w &$$6#&ϦϦʔε࣌ͷ։ൃϦʔμʔ w 4ZNGPOZͰ࠾༻͞Ε͍ͯΔ03. w ಛʹԿ΋ܧঝ͠ͳ͍Ϋϥε͕جຊ w +BWB͔Βଓ͘఻౷తͳ03. ߴڮ৻Ұ

  8. /** * @ORM\Entity * @ORM\Table(name="books") */ class Book { /**

    * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="string", length=100) */ private $title;
  9. $repository = $doctrine ->getRepository(‘BookBundle:Book’); $book = $repository->find(1); echo $book->getTitle(); class

    Book { private $id; private $title; }
  10. $BLF1)103. w ؔ੢Ͱ΋ͬͱ΋$BLF1)1ʹΞπ͍உ w "SSBZΛฦ͍ͯͨ͠$BLF1)1·Ͱͱ͸શ͘ผ ͷ΋ͷ w ΋ͬͱ΋໨৽͍࣮͠૷ͳͷͰ஫໨ ݪా߁ੜ

  11. use Cake\ORM\Entity; class Book extends Entity { // … }

    use Cake\ORM\Table; class BooksTable extends Table { // … } UBCMF5BCMF3FHJTUSZHFU b#PPLT`  CPPLUBCMFHFU   FDIPCPPLUJUMF
  12. ςʔϒϧؔ܎

  13. CPPLT BVUIPST 

  14. SELECT * FROM books LEFT JOIN authors ON authors.id =

    books.author_id
  15. &MPRVFOUͷˎ $book = Book::find(1); echo $book->author->name; class Book extends Model

    { public function author() { return $this->belongsTo(Author::class); } }
  16. &MPRVFOUͷˎ select * from `books` where `books`.`id` = ? limit

    1 select * from `authors` where `authors`.`id` = ? limit 1
  17. $BLF1)1ͷˎ class BooksTable extends Table { public function initialize(array $config)

    { parent::initialize($config); $this->belongsTo('Authors'); } $book = TableRegistry::get('Books')->get(1, [ 'contain' => ['Authors'] ]); echo $book->author->name;
  18. $BLF1)1ͷˎ SELECT Books.id AS `Books__id`, Books.title AS `Books__title`, Books.price AS

    `Books__price`, Books.author_id AS `Books__author_id`, Authors.id AS `Authors__id`, Authors.name AS `Authors__name`, Authors.has_copyright AS `Authors__has_copyright` FROM books Books LEFT JOIN authors Authors ON Authors.id = (Books.author_id) WHERE Books.id = 1 LIMIT 1
  19. %PDUSJOFઃఆʹΑΔ /** * @ORM\ManyToOne(targetEntity="Author", inversedBy="books") * @ORM\JoinColumn(name="author_id", referencedColumnName="id") */ private

    $author; /** * @ORM\ManyToOne(targetEntity="Author", inversedBy="books", fetch="EAGER") * @ORM\JoinColumn(name="author_id", referencedColumnName="id") */ private $author; SELECT t0.id AS id_1, t0.title AS title_2, t0.price AS price_3, t0.author_id AS author_id_4 FROM books t0 WHERE t0.id = 1; SELECT t0.id AS id_1, t0.name AS name_2, t0.has_copyright AS has_copyright_3 FROM authors t0 WHERE t0.id = 1; SELECT t0.id AS id_1, t0.title AS title_2, t0.price AS price_3, t0.author_id AS author_id_4, t5.id AS id_6, t5.name AS name_7, t5.has_copyright AS has_copyright_8 FROM books t0 LEFT JOIN authors t5 ON t0.author_id = t5.id WHERE t0.id = 1;
  20. / ໰୊

  21. &MPRVFOU-B[ZWT&BHFS $books = Book::with('author')->get(); foreach ($books as $book) { echo

    $book->author->name; } select * from `books` select * from `authors` where `authors`.`id` in (1, 2, 3, 4) $books = Book::get(); foreach ($books as $book) { echo $book->author->name; } select * from `books` select * from `authors` where `authors`.`id` = 1 select * from `authors` where `authors`.`id` = 1 select * from `authors` where `authors`.`id` = 1 select * from `authors` where `authors`.`id` = 2 … ͜Ε͕ books ͷ਺͚ͩൃੜ
  22. %PDUSJOF&OUJUZ.BOBHFS &OUJUZ.BOBHFS $book->getAuthor() BVUIPSʜBVUIPSO ϨΠδʔͷൃੜ͸BVUIPS਺·Ͱ

  23. ΦϒδΣΫτಉҰੑ ٯϦϨʔγϣϯ

  24. ಉҰੑ͕ҡ࣋͞ΕΔͷ͔ BVUIPSCPPLT<>BVUIPS 㱠

  25. $BLF1)1ͷ৔߹ BVUIPSCPPLT<>BVUIPS ΫΤϦ࣌ʹDPOUBJOͰࢦఆͨ͠ൣғͷΈଘࡏ͢Δ ॥؀͢Δ৔߹͸ͦ΋ͦ΋ࢦఆࣗମ͕Ͱ͖ͳ͍

  26. ٯϦϨʔγϣϯ :JJ BVUIPS CPPLT JOWFSTFE0G bBVUIPS`   BVUIPS BVUIPS

    CPPLT
  27. %PDUSJOF&OUJUZ.BOBHFS &OUJUZ.BOBHFS $author->getBooks()[0]->getAuthor() BVUIPS …->getAuthor() ಉҰϨίʔυ ৗʹಉ͡Πϯελϯε

  28. ສ݅όονॲཧ

  29. όοϑΝඇόοϑΝ GPSFBDI CPPLTBTCPPL \ ^ GPSFBDI CPPLTBTCPPL \ ^ ($

    ($ GFUDI GSFF GFUDI GSFF Yສ ສϧʔϓ CPPLTΠςϨʔλʔ CPPLT഑ྻ
  30. ʮ03.͕͋Ε͹42-Λ஌Βͳͯ͘΋࡞ΕΔʯ ʮ42-Λ஌ͬͯΕ͹03.͸ͳ͍͍ͯ͘ʯ ͜ΕͲ͏Α

  31. ڧྗ͗͢Δ42- ˣ ߏ଄త੍໿