Codxplore.com

Latest news:

New android application Islamic Video has been released. its a free islamic video application for android smartphone. Download now!

Yii2 custom query



Sample 1:

$model = Yii::$app->db->createCommand('select * from tbl_service_categories
where category_id  = ' . $id)->queryOne();

Sample 2:

$model = Yii::$app->db->createCommand('select * from users)->queryAll();


Views: 807

Yii2 Radio Button List



Sample Code :

field($model, 'notify_provider')->radioList(array('1' => 'Yes', 0 => 'No')); ?>


Views: 1460

Yii2 Multi Select DropDown



in top of your model import array helper class

use yii\helpers\ArrayHelper;

in the model section we can populate the data this way.

public function getServices()
    {
        $model = Services::find()->all();
        $listData=ArrayHelper::map($model,'service_id','name_en');
        return $listData;
    }

in the view section we need to use like that

   
    echo $form->field($model, 'services')->dropDownList($model->getServices(),

   ['class' => 'form-control', 'multiple' => true]);
?>


Views: 3989

Yii2 findBySql() function



findBySql();

$sql = 'Select * from tbl_country';
$country = Country::findBySql($sql)->all();


Views: 1222

Yii2 where query



Sample 1:

Category::find()->where(['category_id' => "1", 'parent_id' => "0"])->one();

Sample 2:

Products::find()->asArray()->where('price between ' . $min . ' and ' . $max . ' ')->all();

Sample 3:

Users::find()->where('email=' . $email)->all();


Views: 831

Yii2 ListBox Field



Using below code we can create the list box base on model attribute of yii2.0 framework. We added the following options like prompt, size, disabled, style etc


field($model, 'population')-> listBox(
            array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
            array('prompt'=>'Select')
            ); ?>

field($model, 'population')-> listBox(
            array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
            array('prompt'=>'Select','size'=>3)
            ); ?>

field($model, 'population')-> listBox(
            array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
            array('disabled' => true,'style'=>'background:gray;color:#fff;'))
            ->label('Gender'); ?>


Views: 1289

Yii2 File Upload & Checkbox



Single File Upload

$form->field($model, 'uploadFile')->fileInput() ?>

MultiFile Upload

echo $form->field($model, 'uploadFile[]')->fileInput(['multiple'=>'multiple']); ?>

Checkbox Button Field


field($model, 'population')->checkbox(); ?>

field($model, 'population')->checkbox(array('label'=>'')); ?>

field($model, 'population')    ->checkbox(array('label'=>''))
                                        ->label('Gender'); ?>

field($model, 'population')->checkbox(array(
                                'label'=>'',
                                'labelOptions'=>array('style'=>'padding:5px;'),
                                'disabled'=>true                                
                                ))
                                ->label('Gender'); ?>

 

 


Views: 1020

Yii2 HTML5 Email Input Field



HTML5 Email Input Field

$form->field($model, 'email')->input('email') ?>


Views: 897

Yii2 Password Input Field



Password Input Field

//Format 1
field($model, 'password')->input('password') ?>
//Format 2
field($model, 'password')->passwordInput() ?>
//Format 3
field($model, 'password')->passwordInput()->hint('Password should be within A-Za-z0-9')->label('Password Hint') ?>


Views: 738

Yii2 Text Input Field



Text Input Field

//Format 1
field($model,'name'); ?>
//Format 2
field($model, 'name')->textInput()->hint('Please enter your name')->label('Name') ?>


Views: 938

How to use Yii2 Textarea



TextArea Field :

 

The model attribute value shal be used as the content in the textarea.

field($model, 'desc')->textarea(); ?>
field($model, 'desc')->textarea()->label('Description'); ?>
field($model, 'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>

 


Views: 1120

Yii ajax form submit



Form code :

 $form = $this->beginWidget('CActiveForm', array(

                        'id' => 'aa--aprvl--setup-_form-form',
                        // Please note: When you enable ajax validation, make sure the corresponding
                        // controller action is handling ajax validation correctly.
                        // See class documentation of CActiveForm for details on this,
                        // you need to use the performAjaxValidation()-method described there.
                        'enableAjaxValidation' => false,
                    ));
                    ?>

                   

                        labelEx($model, 'TBL_NM'); ?>
                        textField($model, 'TBL_NM', array('class' => 'form-control')); ?>
                        error($model, 'TBL_NM'); ?>
                   


                    hiddenField($model, 'APRVL_GRP_MID', array('class' => 'form-control')); ?>


                   

                        labelEx($model, 'IS_ACT'); ?>
                        dropDownList($model, 'IS_ACT', getYesNoArray(), array('class' => 'form-control')); ?>
                        error($model, 'IS_ACT'); ?>
                   


                   

                                                echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('approvalGrpSetting/addSetting')),
                                array(
                                    'data' => 'js:jQuery(this).parents("form").serialize()+
                                     "&request=ajax"',
                                    'success' => 'function(data){
                                        $("#msg").html(data);
                                    }'
                                ),
                                array(
                                    'id' => 'ajaxSubmitBtn',
                                    'name' => 'ajaxSubmitBtn'
                                )
                         );
                        ?>
                   


                        endWidget(); ?>

               

 

Controller code :

public function actionAddSetting()
    {
        //debugPrint($_POST);
        $model = new AA_APRVL_SETUP();

        if (isset($_POST['AA_APRVL_SETUP'])) {
            $model->attributes = $_POST['AA_APRVL_SETUP'];
            $model->MID = Yii::app()->midcodeHelper->getMid('AA_APRVL_SETUP');
            if ($model->save()) {
                echo '

Successfully added

';
            }
            else{
                echo CHtml::errorSummary($model);
            }
        }
    }

 


Views: 968

Subscribe Us


Follow Us