Skip to content

Instantly share code, notes, and snippets.

@pebriana
Last active September 4, 2018 03:47
Show Gist options
  • Save pebriana/94313ee825f60e1bd02f8e0c6b21aeea to your computer and use it in GitHub Desktop.
Save pebriana/94313ee825f60e1bd02f8e0c6b21aeea to your computer and use it in GitHub Desktop.
Apri Yii CopyPaste
//add order by scope (model)
//call MODEL::model()->byiddesc()->find();
<?php
public function scopes() {
return array(
'byiddesc' => array('order' => 'id DESC'),
);
}
?>
// change default gridview to bootstrap gridview
// copas saja
<?php
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'campaign-grid',
'type' => 'striped bordered condensed',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
.....
// Button Gridview
array(
'htmlOptions'=>array('width'=>'','style'=>'text-align:right;min-width:150px'),
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{view} {proses} {history}',
'buttons'=>array(
'view'=>array(
'icon'=>'fa fa-eye',
),
'proses'=>array(
'icon'=>'icon-hand-right',
'url'=>'Yii::app()->createUrl("/promotion/campaign/proses",array("id"=>$data->id))',
'visible'=>'($data->status_approval == "Waiting" && $data->user_approval==Yii::app()->user->id)'
),
'history'=>array(
'label'=>'History Campaign',
'icon'=>'icon-list',
'url'=>'Yii::app()->createUrl("/promotion/campaign/history",array("id"=>$data->id))',
//'click'=>"$('#dlg-update-product').modal()",
'options'=>array('ajax' => array(
'type'=>'POST',
'url'=>"js:$(this).attr('href')",
'success'=>"function(data) {
$('#dlg-history .content-history').html(data);
$('#dlg-history').modal();
}",
),
),
),
),
),
// before save
protected function beforeSave()
{
if ($this->isNewRecord){
....
}
return parent::beforeSave();
}
// register script
Yii::app()->clientScript->registerScript('myjquery', '
$("#").xyz
');
//criteria alias
$criteria=new CDbCriteria;
$criteria->alias = 'tt';
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Condition
//assign the sql query condition
$criteria = new CDbCriteria;
$criteria->condition = 'status =1 OR status=2';
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Distinct
//apply distinct row selection
$criteria = new CDbCriteria;
$criteria->condition = 'comments="P"';
$criteria->distinct=true;
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Group
//Group the result by assign the column name
$criteria = new CDbCriteria;
$criteria->group='postid';
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Having
//Assign the condition for group by filtered value
$criteria = new CDbCriteria;
$criteria->having='comments="P"';
$criteria->group='postid';
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Join
//Join one or more tables with current table
$criteria = new CDbCriteria;
//LEFT JOIN
$criteria->join='LEFT JOIN User c ON c.userid=t.userid';
// OR INNER JOIN
$criteria->join="INNER JOIN tbl_user as user ON(user.userid=t.user_id)";
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Limit, Offset
//Set the limit and offset values for result
$criteria=new CDbCriteria;
$criteria->limit=10;
$criteria->offset=10;
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Order by
//sort the result of query
$criteria=new CDbCriteria;
$criteria->order = "userid";
//Apply To Model
$models = Modelname::model()->findAll($criteria);
//Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Params
//assign the parameter value
$criteria=new CDbCriteria;
$criteria->condition= "userid=:userid";
$criteria->params=array(':userid'=>$userid);
//Apply To Model
$models = Modelname::model()->findAll($criteria);
// OR Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//scopes
//Assign condition or filter the recordes using scores method. scopes method is reusable one.
$criteria=new CDbCriteria;
//One Scope
$criteria->scopes='activepost';
//Many Scope
$criteria->scopes=array('activepost','inactivepost');
//Scope With Parameters
$criteria->scopes=array('activepost'=>array($params));
//Multiple Scope With Parameters
$criteria->scopes=array('activepost'=>array($params1),'inactivepost'=>array($params2));
//Apply To Model
$models = Modelname::model()->findAll($criteria);
// OR Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//Model For Scopes
//model example with scope function
class Post extends CActiveRecord
{
......
public function scopes()
{
return array(
'activepost'=>array(
'condition'=>'status=1',
'order'=>'postid',
),
'inactivepost'=>array(
'condition'=>'status=0',
),
);
}
}
//SELECT
//give the columns name being selected
$criteria=new CDbCriteria;
$criteria->select='t.userid, t.username';
//Apply To Model
$models = Modelname::model()->findAll($criteria);
// OR Apply To CActiveDataProvider
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
//together
//Using this we can join foreign table with primary key table
$criteria=new CDbCriteria;
$criteria->together=true;
With
relational query criteria
$criteria=new CDbCriteria;
$criteria->with=array('user');
return new CActiveDataProvider($model, array( 'criteria'=>$criteria, ));
Output
SELECT COUNT(DISTINCT `t`.`postid`) FROM `post` `t`
LEFT OUTER JOIN `user` `user` ON
(`t`.`userid`=`user`.`userid`)
addBetweenCondition
Using this get the result between two values.
$criteria=new CDbCriteria;
$criteria->addBetweenCondition("t.createdon",$this->date_after,$this->date_before,"AND");
Output
SELECT COUNT(DISTINCT `t`.`userid`) FROM `usermaster` `t`
WHERE (t.createdon BETWEEN :ycp0 AND :ycp1)
Compare
add comparison in query condition
$criteria=new CDbCriteria;
$criteria->compare('year(`EventStartdate`)','>=:'.$currentYear);
$criteria->compare('year(`EventStartdate`)','>='.$currentYear);
$criteria->compare('t.status',$this->status,true);
addCondition
add condition in query
$criteria = new CDbCriteria;
$criteria->addCondition('status=1','AND');
$criteria->addCondition("status='$this->status'");
//Criteria To Model
$models = Modelname::model()->findAll($criteria);
addInCondition
add IN condition in query
$criteria=new CDbCriteria;
$criteria->addInCondition('categoryid',$this->categoryid,true);
$criteria->addInCondition('id',array(1,2,3,4,5,6));
addNotInCondition
add NOT IN condition in query
$criteria->addNotInCondition('categoryid',$this->categoryid,true);
$criteria->addNotInCondition('id',array(1,2,3,4,5,6));
addSearchCondition
add LIKE condition in query
$criteria=new CDbCriteria;
$criteria->addSearchCondition('t.post','yii');
Output
SELECT COUNT(DISTINCT `t`.`postid`) FROM `post` `t` WHERE (t.post LIKE :ycp0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment