Monday, November 11, 2013

MY Model dari Jamie

Pengunaan :
 
Query record 
1. Mengambil semua record dalam table
   $result = $this->article_model->get_all();
   //SELECT * FROM (`articles`)
 
2. Mengambil 1 record berdasarkan ID (primaty key) 
   $row = $this->article_model->get(2);
   // SELECT * FROM (`articles`) WHERE `id` = 2 
 
3. Mengambil beberapa record berdasarkan ID tertentu (primary key)
   $result = $this->article_model->get_many(array(1,3,4));
   //SELECT * FROM (`articles`) WHERE `id` IN (1, 3, 4) 
 
   jika kondisi atau filternya lebih dari satu maka menggunakan Array,
   $this->news_model->get_many_by(array('active' => 1, 'another_column' => 'value'));   
 
5. mengambil 1 record berdasarkan filed tertentu :
   $row = $this->article_model->get_by('title', 'Fuzzy Wuzzy');
   ////SELECT * FROM (`articles`) WHERE `title` = 'Fuzzy Wuzzy' 
  

6. Menghitung Jumlah record dalam table
   $count = $this->article_model->count_all();
   //SELECT COUNT(*) AS `numrows` FROM `articles` 

7. Menghitung jumlah baris berdasarkan kreteria tertentu
   $count = $this->article_model->count_by('title', 'Dumb and boring post');
   //SELECT COUNT(*) AS `numrows` FROM (`articles`) WHERE `title` = 'judul'
 
8. Mengambil record berdasarkan offset dan limit
   $this->post->limit($limit, $offset)->post->get_all(); 


Insert Data 

1. Insert 1 record
   $insert_id = $this->article_model->insert(array('body'=>'Woot!', 'title'=>'My thoughts'), FALSE);
   //INSERT INTO `articles` (`body`, `title`) VALUES ('Woot!', 'My thoughts')
 
2. Insert beberapa record
   $insert_array = array(
  array('body'=>'Hello', 'title'=>'I must be going'),
  array('body'=>"When she's gone", 'title'=>"Ain't no sunshine" ),
 );
   $insert_ids = $this->article_model->insert_many($insert_array, FALSE); 
  //Array ( [0] => 16 [1] => 17 ) //1
  //INSERT INTO `articles` (`body`, `title`) VALUES ('When she\'s gone', 'Ain\'t no sunshine')
 
Update Data
 
1.Update 1 record berdasarkan primary key.
  $update_id = $this->article_model->update(4, array('body'=>'Isi Renungan 1', 'title'=>'Renungan'));
  //1
  //UPDATE `articles` SET `body` = 'Isi renungan 1', `title` = 'renungan 1' WHERE `id` = 4
  
2.Update berdasarkan kreteria tertentu.
  $update_id = $this->article_model->update_by(array('title'=>'My thoughts'), array('body'=>'Having deeper thoughts'));
  //1
  //UPDATE `articles` SET `body` = 'Having deeper thoughts' WHERE `title` = 'My thoughts' 
 
3. Update berdasdarkan beberapa primary KEY
   $update_id = $this->article_model->update_many(array(3,4,5), array('body'=>"Oh! I've been updated...and I feel MARVELOUS!"));
   //1
   //UPDATE `articles` SET `body` = 'Oh! I\'ve been updated...and I feel MARVELOUS!' WHERE `id` IN (3, 4, 5)  

4. Update all data dalam table
  $update_id = $this->article_model->update_all( array('title'=>"Another dumb title"));
  //1
  //UPDATE `articles` SET `title` = 'Another dumb title'
 
Delete Data
 
1. Delete 1 record berdasarkan primary key
   $delete_id = $this->article_model->delete( 7);
   //1
   //DELETE FROM `articles` WHERE `id` = 7 
 
2. Delete berdasarkan kreteria dari field tertentu 
   $delete_id = $this->article_model->delete_by( array('body'=>'Hello'));
   //1
   //DELETE FROM `articles` WHERE `body` = 'Hello' 
3. Delete beberapa  record berdasarkan beberapa primary key 
   $delete_id = $this->article_model->delete_many( array(3,4,5));
   //1
   //DELETE FROM `articles` WHERE `id` IN (3, 4, 5) 
 

No comments:

Post a Comment