Difference of syntax in Yii & Yii 2.0
In this blog i am defining the basic difference of syntax in yii and yii 2.0. so the new user or yii user can easily understand what is the main changes required while making a web application in yii 2.0.
Here, in Yii 2.0 main change is Array() is removed totally from the programming. Instead of array() , yii 2.0 uses [ ] . In Yii only one file is used for model file. For ex.: User.php
While in Yii 2.0 two files required. for example: User.php , UserSearch.php
This are the basic changes required .i have put all the syntax in this table format so that it can easily understand by all users.
Sr. No. | Controll Name | Yii Code | ||
1. | Text Box | Yii 2.0 |
<?= $form->field($model, 'bank_full_name', ['template' => "{label} {input} <span class='status'> </span> "])->textInput() ?> |
|
Yii |
<?php echo $form->labelEx($model,'bank_full_name'); ?> <?php echo $form->error($model,'bank_full_name'); ?> <?php echo $form->textField($model,'bank_full_name',array('size'=>25,'maxlength'=>60)); ?> |
|||
2 | DropdownList | Yii 2.0 |
<?= $form->field($model, 'fees_payment_cheque_bank',['template' => "{label} {input} <span class='status'> </span> "])->dropDownList(ArrayHelper::map(app\modules\fees\models\BankMaster::find()->all(),'bank_id','bank_full_name'),['prompt'=>'--Select Bank--']) ?> |
|
Yii |
<?php echo $form->labelEx($model,'fees_payment_cheque_bank'); ?> <?php echo $form->dropdownList($model,'fees_payment_cheque_bank', BankMaster::items(),array('empty' => 'Select Bank')); ?><span class="status"> </span> <?php echo $form->error($model,'fees_payment_cheque_bank'); ?> |
|||
3 | Date Picker | Yii 2.0 |
<?= $form->field($model, 'batch_start_date', ['template' => "{label} {input} <span class='status'> </span> "])->widget(yii\jui\DatePicker::className(), [ 'clientOptions' =>[ 'dateFormat' => 'dd-mm-yyyy', 'changeMonth'=> true, 'changeYear'=> true, 'autoSize'=>true, 'yearRange'=>'1900:'.(date('Y')+1), 'showOn'=> "button", 'buttonImage'=> Yii::$app->homeUrl."images/calendar.png", 'htmlOptions'=>[ 'style'=>'width:250px;', 'class'=>'form-control', ],]]) ?> |
|
Yii |
<?php echo $form->labelEx($model,'batch_start_date'); ?> <?php echo $form->error($model,'batch_start_date'); ?> <?php if($model->batch_start_date != '' && $model->batch_start_date !='0000-00-00') $model->batch_start_date= date('d-m-Y',strtotime($model->batch_start_date)); else $model->batch_start_date = ''; $this->widget('zii.widgets.jui.CJuiDatePicker', array( 'model'=>$model, 'attribute'=>'batch_start_date', 'options'=>array( //'showOn'=> "button", 'dateFormat'=>'dd-mm-yy', 'changeYear'=>'true', 'changeMonth'=>'true', 'showAnim' =>'slide', 'yearRange'=>'1900:'.(date('Y')+1), 'buttonImage'=>Yii::app()->theme->baseUrl.'/images/calendar.png', ), 'htmlOptions'=>array( 'style'=>'width:250px;vertical-align:top', 'readonly'=>true, ), )); ?><span class="status"> </span> |
|||
4 | Button | Yii 2.0 | <?= Html::a('Cancel', ['/course/course/view?id='.$_REQUEST['courseId']], ['class' => 'btn btn-default']) ?> | |
Yii | echo CHtml::link('Cancel', array('/course/'.$_REQUEST['courseId']), array('class'=>'btnCan')); | |||
5 | Controller & Form File | Yii 2.0 |
in any controller file,_form file if you are uses another model u need to display at top of the file: For Example: in my BatchController.php file i am using Course Model then i have to write the following: use app\modules\course\models\Course; |
|
Yii | No Need | |||
6 | Controller File | Yii 2.0 |
protected function findModel($id) $this->findModel($id)->delete(); |
|
Yii |
public function loadModel($id) $model=$this->loadModel($id); |
|||
7 | Create Command Query | Yii 2.0 |
$batch=$this->batch_name; $course_id=$this->course_id; $query= new \yii\db\Query(); $query -> select(['batch_name']) -> from('batch') -> where('batch_name="'.$batch.'" AND course_id='.$course_id); $command=$query->createCommand(); $batch_name=$command->queryOne(); |
|
Yii |
$batch=$this->batch_name; $course_id=$this->course_id; $batch_name=Yii::app()->db->createCommand() ->select('batch_name') ->from('batch') ->where('batch_name="'.$batch.'" AND course_id='.$course_id) ->queryAll(); |
|||
8 | render / redirect | Yii 2.0 |
return $this->render('create', [ return keyword is used. |
|
Yii |
$this->render('create',array( 'model'=>$model, )); |
|||
9 | Hidden Field | Yii 2.0 | <?= Html::activeHiddenInput($model, 'scenario',['value'=>$model->scenario]); ?> | |
Yii | <?php echo CHtml::hiddenField('scenario',$model->scenario); ?> | |||
10 | Check Box | Yii 2.0 | <?= $form->field($model, 'message_of_day_active', ['template' => "{input} {label} <span class='status'> </span>"])->checkBox(['value'=>1, 'uncheckValue'=>0], true); ?> | |
Yii |
<?php echo $form->labelEx($model,'message_of_day_active'); ?> <?php echo $form->checkBox($model,'message_of_day_active', array('value'=>1, 'uncheckValue'=>0)); ?> <?php echo $form->error($model,'message_of_day_active'); ?> |
|||
11 | TextArea | Yii 2.0 | <?= $form->field($model, 'message',['template' => "{label} {input} <span class='status'> </span> "])->textArea(['maxlength' => 1000]) ?> | |
Yii |
<?php echo $form->textArea($model,'message',array('rows'=>10, 'cols'=>450,'style'=>'float:left;height:100px;width:500px')); ?><span class="status"> </span> <b style="color:red"><?php echo $form->error($model,'message'); ?></b> |
|||
12 | _form.php | Yii 2.0 |
<?php $form = ActiveForm::begin([ 'id' => 'batch-form', 'options' => ['class' => 'form-horizontal'], ]); ?> <?php ActiveForm::end(); ?> |
|
Yii |
<?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'message-of-day-form', 'enableAjaxValidation'=>true, 'clientOptions'=>array('validateOnSubmit'=>true), )); ?> <?php $this->endWidget(); ?> |
|||
13 | Password Field | Yii 2.0 | <?= $form->field($model, 'current_pass',['template' => "{label} {input} <span class='status'> </span>{error} "])->passwordInput(['maxlength' => 60]) ?> | |
Yii |
<?php echo $form->labelEx($model,'current_pass'); ?> <?php echo $form->passwordField($model,'current_pass',array('size'=>50,'maxlength'=>50)); ?><span class="status"> </span> <?php echo $form->error($model,'current_pass'); ?> |
|||
14 | Relation | Yii 2.0 |
public function getRelcourse() { return $this->hasOne(Course::className(), ['course_id' => 'course_id']); } |
|
Yii |
return array( 'Rel_course' => array(self::BELONGS_TO, 'Course','course_id'), ); |
|||
15 | Check Box | Yii 2.0 | Yii::$app->user->can("important-notice/update") | |
Yii | Yii::$app()->user->checkAccess('ImportantNotice.Update') | |||
16 | Data Fetch | Yii 2.0 |
1) $info = StudentInfo::findOne($model->student_transaction_student_id); 2) $info = StudentInfo::find()->where(['student_info_transaction_id'=>$id])->one(); 3) $use_model = User::findOne($model->student_transaction_user_id)->delete(); |
|
Yii |
1) $info = StudentInfo::model()->findByPk($model->student_transaction_student_id); 2) $info = StudentInfo::model()->findByAttributes(array('student_info_transaction_id'=>$id)); 3) $use_model = User::model()->findByPk($model->student_transaction_user_id)->delete(); |
|||