1
- ######
2
- Models
3
- ######
1
+ #####
2
+ Model
3
+ #####
4
4
5
- Models are **optionally ** available for those who want to use a more
6
- traditional MVC approach.
5
+ Models merupakan suatu *opsi * yang tersedia untuk siapa saja yang ingin menggunakan pendekatan tradisional yang lebih pada MVC.
7
6
8
- .. contents :: Page Contents
7
+ .. contents :: Daftar Isi
9
8
10
- What is a Model ?
11
- ================
9
+ Apa itu model ?
10
+ ==============
12
11
13
- Models are PHP classes that are designed to work with information in
14
- your database. For example, let's say you use CodeIgniter to manage a
15
- blog. You might have a model class that contains functions to insert,
16
- update, and retrieve your blog data. Here is an example of what such a
17
- model class might look like::
12
+ Model merupakan class PHP yang didesain untuk hal-hal yang terkait dengan informasi basis data.
13
+ Sebagai contoh, katakanlah Anda menggunakan CodeIgniter untuk mengolah sebuah blog.
14
+ Anda mesti memiliki sebuah class model yang berisi beberapa fungsi (metode) untuk menambah, mengedit, atau menampilkan data blog Anda.
15
+ Berikut ini sebuah contoh seperti apa yang dimaksud dengan class model::
18
16
19
17
class Blog_model extends CI_Model {
20
18
21
- public $title ;
22
- public $content ;
23
- public $date ;
19
+ public $judul ;
20
+ public $konten ;
21
+ public $waktu ;
24
22
25
23
public function __construct()
26
24
{
27
- // Call the CI_Model constructor
25
+ // Memanggil konstruktor CI_Model
28
26
parent::__construct();
29
27
}
30
28
31
- public function get_last_ten_entries ()
29
+ public function data_sepuluh_artikel_terakhir ()
32
30
{
33
- $query = $this->db->get('entries ', 10);
31
+ $query = $this->db->get('artikel ', 10);
34
32
return $query->result();
35
33
}
36
34
37
- public function insert_entry ()
35
+ public function tambahkan_item ()
38
36
{
39
- $this->title = $_POST['title ']; // please read the below note
40
- $this->content = $_POST['content '];
41
- $this->date = time();
37
+ $this->judul = $_POST['judul ']; // lihat catatan di bawah
38
+ $this->konten = $_POST['konten '];
39
+ $this->waktu = time();
42
40
43
- $this->db->insert('entries ', $this);
41
+ $this->db->insert('artikel ', $this);
44
42
}
45
43
46
- public function update_entry ()
44
+ public function sunting_artikel ()
47
45
{
48
- $this->title = $_POST['title '];
49
- $this->content = $_POST['content '];
50
- $this->date = time();
46
+ $this->judul = $_POST['judul '];
47
+ $this->konten = $_POST['konten '];
48
+ $this->waktu = time();
51
49
52
- $this->db->update('entries ', $this, array('id' => $_POST['id']));
50
+ $this->db->update('artikel ', $this, array('id' => $_POST['id']));
53
51
}
54
52
55
53
}
56
54
57
- .. note :: The methods in the above example use the :doc:`Query Builder
58
- <../database/query_builder>` database methods .
55
+ .. note :: Beberapa metode atau fungsi pada contoh di atas menggunakan metode basis data yang dinamakan :doc:`Query Builder
56
+ <../database/query_builder>`.
59
57
60
- .. note :: For the sake of simplicity in this example we're using ``$_POST``
61
- directly. This is generally bad practice, and a more common approach
62
- would be to use the :doc: `Input Library <../libraries/input >`
63
- ``$this->input->post('title ') ``.
58
+ .. note :: Untuk contoh sederhana di sini menggunakan ``$_POST`` secara langsung.
59
+ Umumnya ini praktek yang buruk untuk dilakukan, mengingat akan timbulnya masalah celah keamanan.
60
+ Untuk pendekatan yang lebih baik, sebaiknya menggunakan :doc: `Input Library <../libraries/input >`
61
+ ``$this->input->post('judul ') `` dan sebagainya .
64
62
65
- Anatomy of a Model
66
- ==================
63
+ Susunan sebuah Model
64
+ ====================
67
65
68
- Model classes are stored in your **application/models/ ** directory.
69
- They can be nested within sub-directories if you want this type of
70
- organization.
66
+ Berkas atau class model berada di direktori **application/models/ **.
67
+ Model dapat dikelompokkan di dalam sub-direktori bila model yang Anda inginkan menjadi lebih terorganisir.
71
68
72
- The basic prototype for a model class is this ::
69
+ Bentuk dasar sebuah model digambarkan seperti berikut ini ::
73
70
74
- class Model_name extends CI_Model {
71
+ class Nama_model extends CI_Model {
75
72
76
73
public function __construct()
77
74
{
@@ -80,11 +77,11 @@ The basic prototype for a model class is this::
80
77
81
78
}
82
79
83
- Where **Model_name ** is the name of your class. Class names ** must ** have
84
- the first letter capitalized with the rest of the name lowercase. Make
85
- sure your class extends the base Model class.
80
+ Dimana **Nama_model ** adalah nama class dari class model yang Anda buat.
81
+ Penamaan class ** harus ** dimulai dengan huruf besar (kapital) pada karakter huruf pertama.
82
+ Pastikan model class Anda merupakan turunan dari base Model ( class ** CI_Model ** atau ** MY_Model **).
86
83
87
- The file name must match the class name. For example, if this is your class::
84
+ Antara nama berkas dan nama class harus sama. Sebagai contoh, jika Anda menggunakan model class berikut ::
88
85
89
86
class User_model extends CI_Model {
90
87
@@ -95,89 +92,84 @@ The file name must match the class name. For example, if this is your class::
95
92
96
93
}
97
94
98
- Your file will be this ::
95
+ Maka, nama berkasnya harus seperti ini ::
99
96
100
97
application/models/User_model.php
101
98
102
- Loading a Model
103
- ===============
99
+ Menghubungkan sebuah Model
100
+ ==========================
104
101
105
- Your models will typically be loaded and called from within your
106
- :doc: `controller <controllers >` methods. To load a model you will use
107
- the following method::
102
+ Model pada dasarnya akan dimuat dan dipanggil dari metode atau fungsi yang ada pada
103
+ :doc: `controller <controllers >`. Untuk menghubungkan model, Anda harus menggunakan metode berikut ini::
108
104
109
- $this->load->model('model_name ');
105
+ $this->load->model('nama_model ');
110
106
111
- If your model is located in a sub-directory, include the relative path
112
- from your models directory. For example, if you have a model located at
113
- *application/models/blog/Queries.php * you'll load it using ::
107
+ Jika model yang Anda buat terletak di dalam sebuah sub-direktori, sertakan alamat relatif (* relative path *) dari model yang Anda buat.
108
+ Sebagai contoh, jika model yang Anda miliki berlokasi di
109
+ *application/models/blog/Queries.php * Anda akan menghubungkannya dengan cara ::
114
110
115
111
$this->load->model('blog/queries');
116
112
117
- Once loaded, you will access your model methods using an object with the
118
- same name as your class::
113
+ Ketika terhubung, Anda dapat mengakses metode-metode yang ada pada model menggunakan sebuah objek
114
+ dengan nama yang sama dengan nama model class yang Anda buat sebelumnya ::
119
115
120
- $this->load->model('model_name ');
116
+ $this->load->model('nama_model ');
121
117
122
- $this->model_name->method ();
118
+ $this->nama_model->metode ();
123
119
124
- If you would like your model assigned to a different object name you can
125
- specify it via the second parameter of the loading method::
120
+ Jika Anda ingin menggunakan objek yang berbeda untuk sebuah model, Anda bisa menggunakan penamaan (alias) di parameter kedua::
126
121
127
- $this->load->model('model_name ', 'foobar');
122
+ $this->load->model('nama_model ', 'foobar');
128
123
129
- $this->foobar->method ();
124
+ $this->foobar->metode ();
130
125
131
- Here is an example of a controller, that loads a model, then serves a
132
- view::
126
+ Berikut adalah contoh sebuah controller yang terhubung dengan sebuah model
127
+ dan menampilkan data hasil olahan model ke view::
133
128
134
129
class Blog_controller extends CI_Controller {
135
130
136
131
public function blog()
137
132
{
138
133
$this->load->model('blog');
139
134
140
- $data['query'] = $this->blog->get_last_ten_entries ();
135
+ $data['query'] = $this->blog->data_sepuluh_artikel_terakhir ();
141
136
142
137
$this->load->view('blog', $data);
143
138
}
144
139
}
145
140
146
141
147
- Auto-loading Models
148
- ===================
142
+ * Auto-loading * Model
143
+ ====================
149
144
150
- If you find that you need a particular model globally throughout your
151
- application, you can tell CodeIgniter to auto-load it during system
152
- initialization. This is done by opening the
153
- **application/config/autoload.php ** file and adding the model to the
154
- autoload array.
145
+ Auto-loading (menghubungkan secara otomatis) model tertentu secara global bisa Anda lakukan dengan
146
+ menggunakan pengaturan yang ada pada berkas **application/config/autoload.php **. Tambahkan model yang
147
+ ingin Anda hubungkan secara otomatis selama sistem berjalan::
155
148
156
- Connecting to your Database
157
- ===========================
149
+ $autoload['model'] = array('nama_model');
158
150
159
- When a model is loaded it does ** NOT ** connect automatically to your
160
- database. The following options for connecting are available to you:
151
+ Koneksi ke Basis Data (Database)
152
+ ================================
161
153
162
- - You can connect using the standard database methods :doc: `described
163
- here <../database/connecting>`, either from within your
164
- Controller class or your Model class.
165
- - You can tell the model loading method to auto-connect by passing
166
- TRUE (boolean) via the third parameter, and connectivity settings,
167
- as defined in your database config file will be used::
154
+ Ketika sebuah model sudah terhubung namun tidak dapat terkoneksi ke basis data secara otomatis,
155
+ beberapa opsi yang bisa digunakan untuk mengatasi masalah ini:
168
156
169
- $this->load->model('model_name', '', TRUE);
157
+ - Anda dapat meng-koneksikan dengan menggunakan standar metode database (:doc: `penjelasan di sini <../database/connecting >`),
158
+ antara class Controller atau class Model.
159
+ - Anda dapat mengatur sebuah model melakukan *auto-connect * dengan menambahkan nilai TRUE (boolean) di parameter ketiga.
160
+ Atau mengatur konektivitas sebagaimana yang telah didefinisikan di dalam berkas **application/config/database.php **::
170
161
171
- - You can manually pass database connectivity settings via the third
172
- parameter::
162
+ $this->load->model('nama_model', '', TRUE);
163
+
164
+ - Anda dapat mengatur koneksi secara manual dengan menambahkan item-item berupa array pada parameter ketiga seperti contoh berikut::
173
165
174
166
$config['hostname'] = 'localhost';
175
- $config['username'] = 'myusername ';
176
- $config['password'] = 'mypassword ';
177
- $config['database'] = 'mydatabase ';
167
+ $config['username'] = 'namapengguna ';
168
+ $config['password'] = 'katasandi ';
169
+ $config['database'] = 'nama_database ';
178
170
$config['dbdriver'] = 'mysqli';
179
171
$config['dbprefix'] = '';
180
172
$config['pconnect'] = FALSE;
181
173
$config['db_debug'] = TRUE;
182
174
183
- $this->load->model('model_name ', '', $config);
175
+ $this->load->model('nama_model ', '', $config);
0 commit comments