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

Schema & Migration操作

Schema & Migration操作

Shengyou Fan

December 07, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. ֊ஈ೚຿ • ላशৗ༻ artisan ࢦྩ • ላशݐཱ migration • ላश࢖༻

    Schema Builder ݐཱࢿྉݿ݁ߏ • ಁա artisan ࢦྩࣥߦࢿྉݿ݁ߏతᏓߋ
  2. ೗Կ࢖༻ artisanʁ • $ php artisan {ࢦྩ໊᜝} • ޻࡞๥ፊࢸ໨લҝओɼቮៃ࢖༻աత༗ɿ -

    env - migrate:install • ࡏ઀Լိత֤ᄸݩ㚎ɼሡґෆಉओ୊հ঺ ߋଟ artisan ࢦྩ
  3. artisan env • ଌࢼ Laravel ໨લॴࡏ؀ڥ - artisan ။ճၚ໨લ Laravel

    ໨લత؀ڥᏓ Ꮠઃఆ • ൣྫɿ $  php  artisan  env
  4. artisan key:generate • 㗞ੜ App Ճີۚᮋ - ए࢖༻ composer ݐཱሢҊత࿩ɼଇ။

    ࣗಈࣥߦṜݸࢦྩ • ൣྫɿ $  php  artisan  key:generate Application  key  [xxxxxxxxxxxxxxxxxx]  set  successfully
  5. artisan dump-autoload • ૟ඳݱ༗ሢҊ໨㑚ɼݐཱ class map - artisan ။૟ඳݱ༗తሢҊ໨લࢿྉᇄɼ ฒ೺ॴ༗ྨผٴ䈕໊၏੒Ұݸ

    class map ሣরදɻ(ए༗खಈ৽⃧䈕Ҋ࣌ɼهಘࣥ ߦࠑࢦྩ) • ൣྫɿ $  php  artisan  dump-­‐autoload
  6. ॄኄੋ Migrationʁ • ॴ༗ሣ DB ૢ࡞తಈ࡞ɼ౎Ḥሜሣጯతఔ ࣜᛰɼಁաࣥߦఔࣜိૢ࡞ࢿྉݿᏓߋ • 㑌ݸਓ፤౸ߋ৽൛ຊޙɼಁաࣥߦ migrate

    बՄҎ༗૬ಉత DB ݁ߏɼए༗໰ ୊໵ՄҎ rollback ճ೭લత㐫ଶ • ՄҎ೺ሏᙛ੒ࢿྉݿత൛ຊ߇੍ܥ౷
  7. ዎኄ၏ Migrationʁ • ઌ༻ artisan 㗞ੜ migration 䈕 • ࢖༻

    Laravel Schema Builder ྨผḤሜ migration 䈕త㚎༰ • ࢖༻ artisan ࣥߦ migrate • Մኺ phpMyAdmin ཫ֬ೝࣥߦޙత݁Ռ
  8. artisan migrate:make • 㗞ੜ migration 䈕 - artisan ݪੜࢦྩɼґরڅ༧త migration

    ໊᜝ɼ㗞ੜ migration 䈕ɼฒࣥߦ dump- autoload • ൣྫɿ $  php  artisan  migrate:make  create_posts_table
  9. artisan generate:migration • 㗞ੜ migration 䈕 - generator ൛త㗞ੜ migration

    ࢦྩɼ༏ ᴍࡏԙಁա migration 䈕໊తอཹࣈɼ༬ ઌ㗞ੜ Schema Builder ޠ๏ • ൣྫɿ $  php  artisan  generate:migration  create_posts_table
  10. Migration 䈕Ҋ݁ߏ • ෼੒ up / down (લਐ/ޙୀ) - up

    Ḥሜཁ၏Ꮣߋత㚎༰ (৽⃧ɺ႟আɺ ॏ৽໋໊) - down ੋሜ೗Կሡ up త㚎༰၏ؐݪ (႟ আɺ৽⃧ɺॏ৽໋໊) ˒ ჩߟɿhttp://laravel.tw/docs/migrations
  11. Schema Builder ޠ๏ • ݐཱࢿྉද • ॏ৽໋໊ࢿྉද • ႟আࢿྉද Schema::create('users',

     function($table) {        $table-­‐>increments('id'); }); Schema::rename($from,  $to); Schema::drop('users'); ˒ ޠ๏ჩߟɿhttp://laravel.tw/docs/4.2/schema#creating-and-dropping-tables
  12. Schema Builder ޠ๏ • ݐཱཝҐ • ॏ৽໋໊ཝҐ • ႟আཝҐ Schema::table('users',

     function($table) {        $table-­‐>string('email'); }); Schema::table('users',  function($table) {        $table-­‐>renameColumn('from',  'to'); }); Schema::table('users',  function($table) {        $table-­‐>dropColumn('votes'); }); (ཁֹ֎҆᧋  doctrine/dbal  ౟݅) ˒ ޠ๏ჩߟɿhttp://laravel.tw/docs/4.2/schema#adding-columns
  13. Schema Builder ޠ๏ • ⃧Ճ index • after ޠ๏ ˒

    ޠ๏ჩߟɿhttp://laravel.tw/docs/4.2/schema#adding-indexes $table-­‐>string('email')-­‐>unique(); $table-­‐>string('name')-­‐>after('email');
  14. artisan migrate • ݺڣ artisan ࣥߦ migrate ಈ࡞ - Ḥሜ׬

    migrate 䈕ޙɼཁࣥߦࠑࢦྩ࠽ ။ਖ਼ࣜमվࢿྉݿ - artisan ။ࣗಈґর migrations ࢿྉදత ل㑚ਐߦ൛ຊ߇؅ • ൣྫɿ $  php  artisan  migrate
  15. ଖଞ migrate ࢦྩ • ؐݪ • ճ౸࠷ॳ • ճ౸࠷ॳޙ࠶ॏ䋯Ұ࣍ $

     php  artisan  migrate:rollback $  php  artisan  migrate:reset $  php  artisan  migrate:refresh
  16. খٕ޼ • ࡏ༌ೖ migration ໊᜝࣌ଧࡨࣈዎኄ㭎ʁ - ઌ rollbackɼॏ৽໋໊ Class ໊᜝ٴ䈕Ҋ

    ໊᜝ɼ࠶ࣥߦ dump-autoload ٴ migrate ࢦྩ - ೺㗞ੜग़ိత䈕Ҋखಈ႟আᎃɺࢿྉݿ 㚎ࢿྉද䜤ᎃɼ࠶ॏ৽㗞ੜҰ࣍
  17. খٕ޼ • ෆখ৺೺ migration ࢿྉද࿔ᆦɺ࿔စແ ๏෮ݪ࣌ዎኄ㭎ʁ - ࢼࢼ php artisan

    migrate:reset - ೺ॴ༗ࢿྉදશ෦खಈ႟আɼ࠶ॏ৽䋯 Ұ࣍ php artisan migrate ॏݐॴ༗ࢿྉද
  18. ֊ஈᒾ查ද • ֬ೝ㗞ੜग़ 3 ݸ migration 䈕 • ֬ೝ DB

    ཫ໨લ༗ 4 ݸࢿྉදɼ෼ผҝɿ migrationsɺpostsɺcategoriesɺcomments
  19. ᄸݩখ݁ • ࡏṜݸᄸݩཫɼզ၇㘸໌೗Կ࢖༻ Laravel త Migration ޭೳҝࢿྉݿ၏൛ຊ߇੍ɼ ฒሡ Blog ॴधཁత༗ݸࢿྉදݐཱ׬੒

    • ԼҰݸᄸݩɼզ၇ሡላश೗Կ࢖༻ Laravel త Seeding ޭೳɼڠॿզ၇㗞ੜࢿྉݿ㚎 తଌࢼࢿྉ