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

EC-CUBE3コードリーディング #4

amidaike
March 02, 2016

EC-CUBE3コードリーディング #4

amidaike

March 02, 2016
Tweet

More Decks by amidaike

Other Decks in Programming

Transcript

  1. Doctrineͷ֓ཁ • Doctrineͱ͸PHPͰॻ͔ΕͨO/RϚούʔͰ͢ɻ • ΋͏গ͠ݫີʹઆ໌͢ΔͱDoctrine͸PDOΛར༻ͨ͠ҎԼͷओཁͳϥΠϒϥϦͰߏ੒͞ Ε͓ͯΓɺ૯শͱͯ͠ҰൠతʹO/RϚούʔͱݺ͹Ε͍ͯ·͢ɻ DoctrineCommon : ڞ௨ϥΠϒϥϦ DoctrineDBAL

    : ֤DBMSͷࠩ෼Λٵऩ͢ΔϨΠϠʔ DoctrineORM : O/RϚούʔ • ORM(Object Relational Mapping)ͱ͸ϓϩάϥϛϯάݴޠͷςΫχοΫͰ͋Γɺσʔλ ϕʔεͱΦϒδΣΫτࢦ޲ϓϩάϥϛϯάݴޠͷؒͷඇޓ׵ͳσʔλΛม׵͢Δϓϩά ϥϛϯάٕ๏Ͱ͢ɻMySQL౳ͷσʔλϕʔεͷσʔλΛૢ࡞͠΍͍͢Α͏ʹ PHP Φϒ δΣΫτʹม׵͠·͢ɻ • DBAL(Database Abstraction Layer)ͱ͸PDOͷந৅ϨΠϠʔͰɺσʔλϕʔεͱ΍Γऔ Γ͢ΔAPIΛఏڙ͠·͢ɻ
  2. DoctrineΛ࢖ͬͨςʔϒϧఆٛ • ຊདྷσʔλϕʔεͷςʔϒϧΛ࡞੒͢Δʹ͸create tableจͰSQLจΛ࢖ͬͯ࡞੒͠· ͕͢ɺDoctrine͸Ruby on Rails༝དྷͷmigrationػೳΛ࢖ͬͯσʔλϕʔε؅ཧΛ͠· ͢ɻ • ࣮ࡍʹ͸ҎԼͷखॱΛ࢖ͬͯςʔϒϧΛ࡞੒͠·͢ɻ

    1.ϝλσʔλͱݺ͹ΕΔઃఆϑΝΠϧ(xxxx.dcm.yml)ʹςʔϒϧΛఆٛ
 ˠECCUBEROOT/src/Eccube/Resource/doctrine ʹଘࡏ 2.ymlϑΝΠϧʹରԠ͢ΔΤϯςΟςΟΫϥεΛ࡞੒
 ˠECCUBEROOT/src/Eccube/Entity ʹଘࡏ 3.doctrine migrationػೳΛ࢖ͬͯσʔλϕʔεʹద༻
 ˠymlϑΝΠϧͱEntityΛ΋ͱʹςʔϒϧ࡞੒ɺmigration࣌ͷόʔδϣϯ؅ཧ༻ςʔ ϒϧ΋࡞੒ 4.ςʔϒϧʹมߋ͕ೖΕ͹ɺ1ɺ2ɺ3ͷखॱΛ܁Γฦͯ͠σʔλϕʔεʹద༻
  3. ςʔϒϧΛ࡞੒͠Α͏ • Eccube.Entity.Study.dcm.ymlͷ಺༰
 Eccube\Entity\Study: type: entity table: dtb_study repositoryClass: Eccube\Repository\StudyRepository

    id: id: type: integer nullable: false id: true column: study_id generator: strategy: AUTO options: unsigned: true fields: name: type: string length: 100 url: type: string nullable: true length: 200 create_date: type: datetime nullable: false update_date: type: datetime nullable: false lifecycleCallbacks: { }
  4. ςʔϒϧΛ࡞੒͠Α͏ • EntityΫϥεΛ࡞੒ޙɺ࣮ࡍʹMySQL΍Postgresʹରͯ͠ςʔϒϧ Λ࡞੒͠·͢ɻ࡞੒ํ๏͸2௨Γ͋Γɺmigrationͱݺ͹ΕΔ࢓૊ ΈΛ࢖͏͔ɺdoctrineίϚϯυΛ࢖ͬͯςʔϒϧΛ࡞੒͢Δ͔ͷ2 ௨Γ͋Γ·͢ɻࠓճ͸͕࣌ؒͳ͍ͨΊdoctrineίϚϯυΛ࢖͏ํ๏ Λઆ໌͠·͢ɻ
 
 
 


    
 ɾDB΁ͷϚΠάϨʔγϣϯํ๏ vendor/bin/doctrine orm:schema-tool:update Λ࣮ߦͯ͠migrationՄೳ͔Ͳ͏͔Λ֬ೝ͢Δɻ vendor/bin/doctrine orm:schema-tool:update --dump-sql Λ࣮ߦͯ͠sqlΛ֬ೝ vendor/bin/doctrine orm:schema-tool:update --force Λ࣮ߦͯ͠DB΁ϚΠάϨʔγϣϯ
  5. DoctrineΛ࢖ͬͨDBΞΫηε • DBʹΞΫηε͢ΔͨΊʹ͸Repositoryͱݺ͹ΕΔΫϥεΛ࢖͍·͢ɻ • Repository͸ಛఆͷΫϥεͷΤϯςΟςΟͷݕࡧΛิॿ͢ΔͨΊ͚ͩͷ PHPΫϥεͰ͢ɻࢦఆ͞ΕͨΤϯςΟςΟΛऔಘ͢ΔͨΊʹRepository Λ࢖͍·͢ɻ • ymlϑΝΠϧʹrepositoryClass: Eccube\Repository\XXXXRepositoriͱ

    هड़͠ͳ͚Ε͹σϑΥϧτͰ͸Doctrine\ORM\EntityRepositoryΫϥε ͕ར༻͞Ε·͢ɻ
 
 
 Eccube\Entity\Product: type: entity table: dtb_product repositoryClass: Eccube\Repository\ProductRepository ← ͜ΕͰ͢
  6. DoctrineΛ࢖ͬͨDBΞΫηε • EntityRepositoryΫϥεʹ͸ඪ४Ͱ༻ҙ͞Ε͍ͯΔศརͳϝ ιου͕͋Γ·͢ɻΑ͘࢖͏ϝιουʹ͸ҎԼͷ΋ͷ͕͋Γ ·͢ɻ find : ओΩʔΛݩʹݕࡧ݁ՌΛऔಘ findAll :

    શ݅औಘ findBy : ͋Δ৚݅Λݩʹݕࡧ findOneBy : ͋Δ৚݅Λݩʹݕࡧ͢Δ͕݁Ռ͸1݅ͷΈ • ΋ͬͱෳࡶͳ৚͕݅ඞཁͳ৔߹ɺ֤RepositoryΫϥεʹϝ ιουΛ௥Ճ͍ͯͩ͘͠͞ɻ
  7. DoctrineΛ࢖ͬͨDBΞΫηε • RepositoryΫϥεʹ༻ҙ͞Ε͍ͯΔfindϝιουͳͲΛ࢖͑͹جຊతͳΫΤϦͰ ͋Ε͹؆୯ʹDB͔Β஋Λऔಘ͢Δ͜ͱ͕ग़དྷ·͕ͨ͠ɺΑΓෳࡶͳΫΤϦ΋ѻ ͏͜ͱ͕Ͱ͖·͢ɻ • ΑΓෳࡶͳΫΤϦΛѻ͏৔߹ɺDQL(Doctrine Query Language) Λ࢖༻ͯ͠ॻ͘

    ͜ͱ΋ՄೳͰ͢ɻDQL͸SQLͱࣅ͍ͯ·͕͢ɺςʔϒϧ(ྫ: product)ͷߦͰ͸ͳ ͯ͘ɺ1ͭҎ্ͷΤϯςΟςΟΫϥεΦϒδΣΫτ(ྫ: Product)ʹରͯ͠ΫΤϦ ͢Δͱ͍͏͜ͱΛ૝ఆ͠·͢ɻ • DoctrineͰΫΤϦ͢Δʹ͸2ͭͷબ୒ࢶ͕͋Γ·͢ɻ७ਮͳDoctrineΫΤϦΛॻ ͔͘ɺDoctrineͷQueryBuilderΛ࢖༻͢Δ͜ͱͰ͢ɻ
 
 http://docs.symfony.gr.jp/symfony2/book/doctrine.html#book-doctrine-queries
  8. DoctrineΛ࢖ͬͨDBΞΫηε • DoctrineΫΤϦͷྫ
 DoctrineΫΤϦΛ࢖͏ͱ΄΅SQLͱಉ༷ͷ಺༰Ͱهड़͕ ग़དྷ·͢ɻ
 $query = $app['eccube.repository.product'] ->createQuery( 'SELECT

    p FROM Eccube\Entity\Product p WHERE p.name = :name ORDER BY p.update_date DESC' ) ->setParameter('name', 'hoge'); $Products = $query->getResult();
  9. EC-CUBE3ίʔυϦʔσΟϯά • AdminσΟϨΫτϦ௚Լʹ͸؅ཧը໘༻ίϯτϩʔ ϥʔ͕ଘࡏ σΟϨΫτϦ໊ ػೳ໊ AdminController ϩάΠϯը໘ɺ؅ཧը໘τοϓ Content ίϯςϯπ؅ཧ༻

    Customer ձһ؅ཧ༻ Order ड஫؅ཧ༻ Product ঎඼؅ཧ༻ Setting جຊ৘ใઃఆɺγεςϜ৘ใઃఆ༻ Store ΦʔφʔζετΞ༻