[YII] Basic CRUD


Description

        Memahami basic dari Create , Read , Update, and Delete.

Skill : *  Basic SQL - DML (Data Manipulation).

           * Basic Form , Controller , Model YII

How To :
        Untuk membuat agar web yang dibuat menggunakan framework YII bisa input data , edit data , atau  delete data sebetulnya tidak harus memahami penulisan syntax SQL karena memang sudah disediakan. tetapi pemahaman untuk SQL wajib didalami jika suatu saat ada kondisi yang tidak memungkinkan untuk menggunakan library dari framework YII ini.

       Untuk bisa melakukan CRUD pada YII , bisa dihadapkan 2 pilihan langsung yaitu mengakses classnya si model secara langsung (namescope) atau merubah dulu menjadi object, terserah sih itu pilihan anda tapi kebanyakan  rata menggunakan namescope mengakses classnya si model secara langsung tapi terkadang ada case" tertentu untuk menggunakan object. tapi di tutorial ini saya lebih menggunakan object karena sudah terbiasa dengan Framework Cakephp haha, ok to the point !


Our Setup :

  - Database Name : yii
  - Table Name : yiitest
     - Field 1 : id
     - Field 2 : name
     - Field 3 : description
  - Yii Model Name : Yiitest
  - Yii Controller Name : contoh

BASIC INPUT 

example:

class contohController extends Controller
{
     ...........another code ...........
     ...........another code ...........


       public function actionInsertion()
       {

       $AbstractDB = new Yiitest

       $AbstractDB ->name='Griffindor';
       if ($AbstractDB ->save())
       {
           echo 'Save Sukses';
       }
       else
       {
echo 'Save Failure'.'<br>';
$error = $AbstractDB ->getErrors();
        print_r($error);
       }
       $this->render('Insertion');
       }
}



BASIC EDIT

class ContohController extends Controller
{
     ...........another code ...........
     ...........another code ...........


       public function actionUpdating()
       {
               $AbstractDB = new Yiitest;
                // $AbstractDB ->updateByPk(PK_VALUE, array('FIELD'=>'NEW_VALUE'));
$AbstractDB ->updateByPk(1, array('description'=>'sukses bro'));
unset($DbAr);
       }
}




BASIC DELETE

class ContohController extends Controller
{
     ...........another code ...........
     ...........another code ...........


       public function actionDeleting()
       {
$AbstractDB =Yiitest::model();
$store= $AbstractDB ->find("name=:values",array(':values'=>'ravenclaw'));
$AbstractDB ->deleteByPk($store['id']);
               // hey look at ':values' the values refers to  ':values'=>'ravenclaw'
       }
}



BASIC READ
example:
class ContohController extends AppController
{
   ...........another code ...........
   ...........another code ...........


 public function actionReading()

 {
     // to get data from database we use CActiveDataProvider Class
     $dataProvider = new CActiveDataProvider('Yiitest');
     $resultSet = $dataProvider->getData();
     foreach ($resultSet as $datax)
     {
          echo $datax['id'].' '.$datax['name'].' '.$datax['description'].'<br>';
     }
  }
}

if you want datalist for value object Html like a list box you use this :

class ContohController extends AppController
{
   ...........another code ...........
   ...........another code ...........


 public function actionReading()

 {
     // to get data from database we use CActiveDataProvider Class
     $data = Yiitest::model()->findAll();
     $datacache = CHtml::listData($data,'id','name');
     print_r ($datacache);
     // the data looks like this 
     //Array ( [1] => mahendra )
     // [1] is value in field id
     // [mahendra] is value in field name 
  }
}

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih