-->

Yii Code Snippet Collection (Yii 1 Only) part 1

Sudah lama gak koding yii framework dan setelah bedah bedah dokumentasi ane menemukan tulisan ini nih. Mending dishare daripada nanti lupa nyariin lagi semoga bermanfaat ya teman teman :


Cara Menggunakan CActiveDataProvider di YiiFramework


Cara Membuat CActiveDataprovider
<?php $high_scores_dp =
 new CActiveDataProvider('score',
  array('criteria' =>
   array('condition' => 'game_id = ' . $model->id,
      'order' => 'score desc',
      'limit' => 20,     //  <=== TRIED CHANGING THIS VALUE
     )
    )
    , 'pagination' => false            // <=== FIXED BY ADDING THIS LINE

   );
      
?>

Membuat CActiveDataprovider dengan Criteria


<?php
 criteria = new CDbCriteria;
 $criteria->with = array('foreign_table1','foreign_table2', 'foreign_table2.foreign_table3');
 $criteria->select = array('id');
 $criteria->condition = "foreign_table1.col1=:col_val AND foreign_table3.col3=:col_val2";
 $criteria->params = array(':col_val' => some_val, ':col_val2' => other_val);
 $criteria->order = 'foreign_table3.col5 DESC';
 $criteria->limit = 10;
 //with criteria
 
 
 $dataProvider=new CActiveDataProvider('Post', array(
  //'criteria'=>$criteria, //if using criteria
  'criteria'=>array(
   'condition'=>'status=1 AND post_id='.$model->post_id,
   'order'=>'create_time DESC',
   'with'=>array('author'),
   'limit'=>20
  ),
  'pagination'=>array(
   'pageSize'=>20,
  ),
 ));

      $dataProvider->getData() will return a list of Post objects


ACTIVE RECORDS

 Menggunakan FindByAttributes di Yiiframework


<?php
 $usersComments=KomentarThread::model()->findAllByAttributes(array('ID_THREAD'=>$model->ID_THREAD),array(
  'select'=>'t.ID_USER',
  'condition'=>'t.ID_USER<>:id_user_pembuat AND t.ID_USER<>:id_user_komentar',
  'params'=>array(':id_user_pembuat'=>$idUserPembuat,':id_user_komentar'=>$model->ID_USER),
  'distinct'=>true,
 ));

?>

 Cara menggunakan Exists di YiiFramework
 <?php
   $this->findByAttributes(array('email'=>$this->email));
   return $this->exists('email = :email', array(':email'=>$this->email));
 ?>

Cara menggunakan Find di YiiFramework


<?php
return User::model()->active()
 ->validemail()
 ->completeprofile()
 ->with('userprofile', 'userskillconfigs')
 ->together()
 ->findAll(
  array('condition' => 'id_gametype='.Gametype::MAUMAU, 
   'limit' => 3, 
   'order' => 'created DESC') );
?>

<?php
$post=Post::model()->find(array(
 'select'=>'title',
 'condition'=>'postID=:postID',
 'params'=>array(':postID'=>10),
));
<?php
// find all rows satisfying the specified condition
$posts=Post::model()->findAll($condition,$params);
// find all rows with the specified primary keys
$posts=Post::model()->findAllByPk($postIDs,$condition,$params);
// find all rows with the specified attribute values
$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
// find all rows using the specified SQL statement
$posts=Post::model()->findAllBySql($sql,$params);


<?php
// get the number of rows satisfying the specified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specified SQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfying the specified condition
$exists=Post::model()->exists($condition,$params)
?>

Cara menyimpan data di YiiFramework


<?php 
$post=new Post;
$post->title='sample post';
$post->content='content for the sample post';
$post->create_time=time();
$post->save();
?>

Cara Update data di YiiFramework
<?php
$post=Post::model()->findByPk(10);
$post->title='new post title';
$post->save(); // s
 
// update the rows matching the specified condition
Post::model()->updateAll($attributes,$condition,$params);
// update the rows matching the specified condition and primary key(s)
Post::model()->updateByPk($pk,$attributes,$condition,$params);
// update counter columns in the rows satisfying the specified conditions
Post::model()->updateCounters($counters,$condition,$params);
?>
Cara menghapus data di YiiFramework
<?php
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10
$post->delete(); // delete the row from the database table


// delete the rows matching the specified condition
Post::model()->deleteAll($condition,$params);
// delete the rows matching the specified condition and primary key(s)
Post::model()->deleteByPk($pk,$condition,$params);
?>


Menggunakan CDBCRITERIA di YiiFramework


Cara menggunakan find dengan cdbcriteria di YiiFramework


<?php
 $match = addcslashes($match, '%_'); // escape LIKE's special characters
 $q = new CDbCriteria( array(
  'condition' => "content LIKE :match",         // no quotes around :match
  'params'    => array(':match' => "%$match%")  // Aha! Wildcards go here
 ) );

 $comments = Comments::model()->findAll( $q );     // works!
?>

Cara menggunakan findAll dengan CdbCriteria di YiiFramework

<?php
$match = addcslashes($match, '%_'); // escape LIKE's special characters

// directly into findAll()
$comments = Comments::model()->findAll(
 'content LIKE :match',
 array(':match' => "%$match%")
);
 
// merging in a parameterized scope:
$this->getDbCriteria()->mergeWith( array(
 'condition' => 'content LIKE :match',
 'params'    => array(':match' => "%$match%")
) );
?>

Kombinasi CDbCriteria dan Find di ActiveRecord

<?php
$q = new CDbCriteria();
$q->addSearchCondition('content', $match);
$comments = Comments::model()->findAll( $q );
?>

<?php
$criteria=new CDbCriteria;
$criteria->select='title';  // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria);
?>
Facebook Comments

0 komentar