Langkah 6: Database Proyek
Saya telah memilih MySQL sebagai DBMS saya, tapi ingat bahwa Kohana juga mendukung MSSQL, MySQLi, PostgreSQL, PDOSqlite.
Membuat database yang disebut "cd_collection" atau memilih nama yang
Anda sukai, dan menjalankan SQL berikut melalui phpMyAdmin atau alat
untuk menangani administrasi MySQL.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >CREATE TABLE `albums` (</span> CREATE TABLE `albums` (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`id` int(11) NOT NULL auto_increment,</span> `Id` int (11) NOT NULL auto_increment,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`name` varchar(50) collate utf8_bin NOT NULL,</span> `Name` varchar (50) menyusun utf8_bin NOT NULL,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`author` varchar(50) collate utf8_bin NOT NULL,</span> `Author` varchar (50) menyusun utf8_bin NOT NULL,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`genre_id` int(11) NOT NULL,</span> `Genre_id` int (11) NOT NULL,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >PRIMARY KEY (`id`),</span> PRIMARY KEY (`id`),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >KEY `genre_id` (`genre_id`)</span> KEY `genre_id` (` genre_id`)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=19 ;</span> ) ENGINE = InnoDB DEFAULT CHARSET = utf8 Collate = utf8_bin AUTO_INCREMENT = 19;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >INSERT INTO `albums` (`id`, `name`, `author`, `genre_id`) VALUES</span> INSERT INTO `albums` (` id`, `name`,` author`, `genre_id`) VALUES</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(2, 'Lines, Vines And Trying Times' , 'Jonas Brothers' , 16),</span> (2, 'Lines, Vines dan Mencoba Times' , 'Jonas Brothers' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(3, 'The END' , 'The Black Eyed Peas' , 16),</span> (3, 'The END' , 'The Black Eyed Peas' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(4, 'Relapse' , 'Eminem' , 18),</span> (4, 'Relapse' , 'Eminem' , 18),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(5, 'Monuments And Melodies' , 'Incubus' , 1),</span> (5, 'Monumen Dan Melodies' , 'Incubus' , 1),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(6, 'Thriller' , 'Michael Jackson' , 16),</span> (6, 'Thriller' , 'Michael Jackson' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(7, 'Back in Black' , 'AC/DC' , 4),</span> (7, 'Back in Black' , 'AC / DC' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(8, 'The Dark Side of the Moon' , 'Pink Floyd' , 4),</span> (8, 'The Dark Side of the Moon' , 'Pink Floyd' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(9, 'Bat out of Hell' , 'Meat Loaf' , 4),</span> (9, 'Bat dari Neraka' , 'Meat Loaf' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(10, 'Backstreet Boys' , 'Millennium' , 16),</span> (10, 'Backstreet Boys' , 'Millenium' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(11, 'Rumours' , 'Fleetwood Mac' , 4),</span> (11, 'Rumor' , 'Fleetwood Mac' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(12, 'Come on Over' , 'Shania Twain' , 16),</span> (12, 'Ayo Over' , 'Shania Twain' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(13, 'Led Zeppelin IV' , 'Led Zeppelin' , 4),</span> (13, 'Led Zeppelin IV' , 'Led Zeppelin' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(14, 'Jagged Little Pill' , 'Alanis Morissette' , 4),</span> (14, 'Jagged Little Pill' , 'Alanis Morissette' , 4),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(15, 'Sgt. Pepper' 's Lonely Hearts Club Band' , 'The Beatles' , 16),</span> (15, 'Sgt. Pepper Lonely Hearts Club Band' , 'The Beatles' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(16, 'Falling into You' , 'C√©line Dion' , 16),</span> (16, 'Jatuh ke Anda' , 'C√ © garis Dion' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(17, 'Music Box' , 'Mariah Carey' , 16),</span> (17, 'Music Box' , 'Mariah Carey' , 16),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(18, 'Born in the USA' , 'Bruce Springsteen' , 4);</span> (18, 'Lahir di Amerika Serikat' , 'Bruce Springsteen' , 4);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >CREATE TABLE `genres` (</span> CREATE TABLE `genres` (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`id` int(11) NOT NULL auto_increment,</span> `Id` int (11) NOT NULL auto_increment,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >`name` varchar(50) collate utf8_bin NOT NULL,</span> `Name` varchar (50) menyusun utf8_bin NOT NULL,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >PRIMARY KEY (`id`),</span> PRIMARY KEY (`id`),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >UNIQUE KEY `name` (`name`)</span> KUNCI UNIK `name` (` name`)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=22 ;</span> ) ENGINE = InnoDB DEFAULT CHARSET = utf8 Collate = utf8_bin AUTO_INCREMENT = 22;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >INSERT INTO `genres` (`id`, `name`) VALUES</span> INSERT INTO `genres` (` id`, `name`) VALUES</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(1, 'Alternative Rock' ),</span> (1, 'Alternatif Rock' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(2, 'Blues' ),</span> (2, 'Blues' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(3, 'Classical' ),</span> (3, 'Klasik' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(4, 'Rock' ),</span> (4, 'Rock' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(5, 'Country' ),</span> (5, 'Negara' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(6, 'Dance' ),</span> (6, 'Dance' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(7, 'Folk' ),</span> (7, 'Folk' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(8, 'Metal' ),</span> (8, 'Logam' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(9, 'Hawaiian' ),</span> (9, 'Hawaii' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(10, 'Imports' ),</span> (10, 'Impor' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(11, 'Indie Music' ),</span> (11, 'Indie Music' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(12, 'Jazz' ),</span> (12, 'Jazz' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(13, 'Latin' ),</span> (13, 'Latin' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(14, 'New Age' ),</span> (14, 'New Age' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(15, 'Opera' ),</span> (15, 'Opera' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(16, 'Pop' ),</span> (16, 'Pop' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(17, 'Soul' ),</span> (17, 'Soul' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(18, 'Rap' ),</span> (18, 'Rap' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(20, 'Soundtracks' ),</span> (20, 'Soundtrack' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(21, 'World Music' );</span> (21, 'World Music' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >ALTER TABLE `albums`</span> ALTER TABLE `albums`</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >ADD CONSTRAINT `genre_inter_relational_constraint` FOREIGN KEY (`genre_id`) REFERENCES `genres` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;</span> ADD CONSTRAINT `genre_inter_relational_constraint` FOREIGN KEY (` genre_id`) REFERENSI `genres` (` id`) ON DELETE CASCADE ON UPDATE CASCADE;</span> |
Seperti yang Anda lihat, SQL menciptakan dua tabel, album dan genre, dan Mempopulai mereka dengan beberapa data. Pernyataan SQL lalu menambahkan kendala untuk asing kunci "genre_id".
Struktur database sangat sederhana dan tidak memerlukan banyak penjelasan.
Sekarang, Anda harus memberitahu Kohana dimana database Anda berada dan bagaimana untuk mengaksesnya. Mengedit global file konfigurasi sistem / config / database.php sebagai berikut
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $config [ 'default' ] = array </span> $ Config [ 'default' ] = array </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(</span> (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'benchmark' => TRUE,</span> 'Patokan' => TRUE,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'persistent' => FALSE,</span> 'Gigih' => SALAH,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'connection' => array </span> 'Koneksi' => array </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >(</span> (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'type' => 'mysql' ,</span> 'Jenis' => 'mysql' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'user' => 'root' ,</span> 'User' => 'root' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'pass' => 'root' ,</span> 'Pass' => 'root' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'host' => 'localhost' ,</span> 'Host' => 'localhost' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'port' => FALSE,</span> 'Pelabuhan' => SALAH,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'socket' => FALSE,</span> 'Socket' => SALAH,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'database' => 'cd_collection' </span> 'Database' => 'cd_collection' </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >),</span> ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'character_set' => 'utf8' ,</span> 'Character_set' => 'utf8' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'table_prefix' => '' ,</span> 'Table_prefix' => '' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'object' => TRUE,</span> 'Objek' => TRUE,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'cache' => FALSE,</span> 'Cache' => SALAH,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'escape' => TRUE</span> 'Melarikan diri' => BENAR</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> |
Kode ini memberitahu Kohana untuk terhubung ke database MySQL yang
disebut "cd_collection" di localhost dengan username "root" dan password
"root". Anda harus mengubah pengaturan ini sesuai dengan konfigurasi server database Anda.
Kohana: The Swift PHP Framework Langkah 7: Buat Controller
Kohana: The Swift PHP Framework
Kohana: The Swift PHP Framework Langkah 10: Final Thoughts
Kohana: The Swift PHP Framework Langkah 9: Buat Proyek View
Kohana: The Swift PHP Framework Langkah 8: Buat Model Project
Kohana: The Swift PHP Framework Langkah 7: Buat Controller
Kohana: The Swift PHP Framework Langkah 6: Database Proyek
Kohana: The Swift PHP Framework Langkah 5: pertama Kohana Proyek Anda
Kohana: The Swift PHP Framework Langkah 4: Konfigurasi Kohana
Kohana: The Swift PHP Framework Langkah 3: Instalasi Kohana
Kohana: The Swift PHP Framework Langkah 2: Men-download Kohana
Kohana: The Swift PHP Framework Langkah 1: Apa Kohana?
0 komentar:
Posting Komentar
silahkan komentar