Langkah 8: Buat Model Project
Sekarang mari kita membuat model untuk aplikasi web kami. Ingat konvensi ini.
- Model nama file harus huruf kecil, misalnya album.php
- kelas model harus memetakan dengan nama file dan dikapitalisasi, dan harus ditambahkan dengan _Model, misalnya Album_Model
- harus memiliki kelas Model seperti (grand) orang tua
Berikut adalah kode model yang album. Buat file bernama album.php dalam aplikasi / model / dan paste kode di bawah ini di atasnya.
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 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" ><?php defined( 'SYSPATH' ) OR die ( 'No direct access allowed.' );</span> <? Php didefinisikan ( 'SYSPATH' ) ATAU mati ( 'Tidak ada akses langsung diperbolehkan.' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class Album_Model extends Model</span> kelas Album_Model meluas Model</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" > private $album_table ;</span> $ album_table swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $genre_table ;</span> $ genre_table swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function __construct()</span> fungsi publik __construct ()</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" >parent::__construct();</span> parent :: __ construct ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_table = 'albums' ;</span> $ This-> album_table = 'album' ;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->genre_table = 'genres' ;</span> $ This-> genre_table = 'genre' ;</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" > public function read( $id )</span> fungsi publik membaca ($ id)</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" > $this ->db->where( 'id' , $id );</span> $ This-> db-> mana ( 'id' , $ id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $query = $this ->db->get( $this ->album_table);</span> $ Query = $ this-> db-> get ($ this-> album_table);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $query ->result_array();</span> kembali $ query-> result_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" > public function delete ( $id )</span> fungsi publik menghapus ($ id)</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" > $this ->db-> delete ( $this ->album_table, array ( 'id' => $id ));</span> $ This-> db-> delete ($ this-> album_table, array ( 'id' => $ id));</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" > public function update( $id , $data )</span> pembaruan fungsi publik ($ id, $ data)</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" > $this ->db->update( $this ->album_table, $data , array ( 'id' => $id ));</span> $ This-> db-> update ($ this-> album_table, $ data, array ( 'id' => $ id));</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" > public function create( $data )</span> fungsi publik membuat ($ data)</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" > $this ->db->insert( $this ->album_table, $data );</span> $ This-> db-> insert ($ this-> album_table, $ data);</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" > public function get_list()</span> fungsi publik get_list ()</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" > $this ->db->select( 'albums.id as id,albums.name as name,albums.author as author, genres.name as genre' );</span> $ This-> db-> pilih ( 'albums.id sebagai id, albums.name nama, albums.author sebagai penulis, genres.name sebagai genre' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->from( $this ->album_table);</span> $ This-> db-> dari ($ this-> album_table);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->join( $this ->genre_table, 'genres.id' , 'albums.genre_id' );</span> $ This-> db-> bergabung ($ this-> genre_table, 'genres.id' , 'albums.genre_id' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $query = $this ->db->get();</span> $ Query = $ this-> db-> get ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $query ->result_array();</span> kembali $ query-> result_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" >}</span> }</span> |
Semua metode dalam model menggunakan sintaks pembangun Query. Alat Kohana ini mempercepat waktu pengembangan database dan menyederhanakan pembuatan query.
Dua anggota variabel dideklarasikan di bagian atas kelas:
1 2 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $album_table ;</span> $ album_table swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $genre_table ;</span> $ genre_table swasta;</span> |
Anggota adalah pribadi karena saya ingin membatasi visibilitas hanya untuk kelas ini. Mereka adalah wadah untuk nama tabel database.
Baris pertama dalam metode konstruktor memuat database perpustakaan Kohana ke $ this-> db. Dalam garis kedua dan ketiga dua anggota kelas diinisialisasi.
1 2 3 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >parent::__construct();</span> parent :: __ construct ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_table = 'albums' ;</span> $ This-> album_table = 'album' ;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->genre_table = 'genres' ;</span> $ This-> genre_table = 'genre' ;</span> |
Query dalam metode membaca mengambil catatan album yang memiliki identifier tertentu ("$ id").
1 2 3 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->where( 'id' , $id );</span> $ This-> db-> mana ( 'id' , $ id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $query = $this ->db->get( $this ->album_table);</span> $ Query = $ this-> db-> get ($ this-> album_table);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $query ->result_array();</span> kembali $ query-> result_array ();</span> |
Query dalam metode delete menghapus baris tabel album yang memiliki identifier tertentu ("$ id").
1 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db-> delete ( $this ->album_table, array ( 'id' => $id ));</span> $ This-> db-> delete ($ this-> album_table, array ( 'id' => $ id));</span> |
Query dalam metode update update baris tabel album yang memiliki
identifier tertentu ("$ id") dengan nilai-nilai baru dari "$ data"
array.
1 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->update( $this ->album_table, $data , array ( 'id' => $id ));</span> $ This-> db-> update ($ this-> album_table, $ data, array ( 'id' => $ id));</span> |
The "$ data" array harus berisi nama catatan sebagai kunci dari array, dan nilai sebagai nilai-nilai dari array. The "$ data" array harus memiliki formulir ini:
1 2 3 4 5 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $data = array (</span> $ Data = array (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'name' => 'album_name' ,</span> 'Nama' => 'ALBUM_NAME' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'author' => 'author_name' ,</span> 'Penulis' => 'AUTHOR_NAME' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'genre_id' => 'genre_id' </span> 'Genre_id' => 'genre_id' </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> |
Query dalam menciptakan metode menyisipkan rekor baru dengan nilai-nilai "$ data" array.
1 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->insert( $this ->album_table, $data );</span> $ This-> db-> insert ($ this-> album_table, $ data);</span> |
The "$ data" array harus memiliki formulir ini:
1 2 3 4 5 6 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $data = array (</span> $ Data = array (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'id' => 'album_id' ,</span> 'Id' => 'album_id' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'name' => 'album_name' ,</span> 'Nama' => 'ALBUM_NAME' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'author' => 'author_name' ,</span> 'Penulis' => 'AUTHOR_NAME' ,</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'genre_id' => 'genre_id' </span> 'Genre_id' => 'genre_id' </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> |
Query dalam metode get_list mengambil semua album baris.
1 2 3 4 5 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->select( 'albums.id as id,albums.name as name,albums.author as author, genres.name as genre' );</span> $ This-> db-> pilih ( 'albums.id sebagai id, albums.name nama, albums.author sebagai penulis, genres.name sebagai genre' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->from( $this ->album_table);</span> $ This-> db-> dari ($ this-> album_table);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->db->join( $this ->genre_table, 'genres.id' , 'albums.genre_id' );</span> $ This-> db-> bergabung ($ this-> genre_table, 'genres.id' , 'albums.genre_id' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $query = $this ->db->get();</span> $ Query = $ this-> db-> get ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $query ->result_array();</span> kembali $ query-> result_array ();</span> |
Sekarang, model bergenre. Buat file bernama genre.php dalam aplikasi / model / dan paste kode di bawah ini:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" ><?php defined( 'SYSPATH' ) OR die ( 'No direct access allowed.' );</span> <? Php didefinisikan ( 'SYSPATH' ) ATAU mati ( 'Tidak ada akses langsung diperbolehkan.' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class Genre_Model extends Model</span> kelas Genre_Model meluas Model</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" > private $genre_table ;</span> $ genre_table swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > function __construct()</span> fungsi __construct ()</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" >parent::__construct();</span> parent :: __ construct ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->genre_table = 'genres' ;</span> $ This-> genre_table = 'genre' ;</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" > function get_list()</span> Fungsi get_list ()</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" > $query = $this ->db->get( $this ->genre_table);</span> $ Query = $ this-> db-> get ($ this-> genre_table);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $query ->result_array();</span> kembali $ query-> result_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" >}</span> }</span> |
Model ini sangat sederhana sehingga saya akan membuang-buang waktu lagi untuk mengomentari itu. Model dan controller siap untuk pergi. Mari kita sekarang bekerja pada Tampilan.
Kohana: The Swift PHP Framework Langkah 9: Buat Proyek View
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