Showing posts with label yii2. Show all posts
Showing posts with label yii2. Show all posts

Sunday, March 12, 2017

Save multiple textInput Array value using Yii2


controller action:

namespace app\controllers;

use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Experienceskills;

    public function actionExperience()
    {
        $settings = Experienceskills::find()->indexBy('id')->all();
        if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
            foreach ($settings as $setting) {
                $setting->save(false);
            }
            Yii::$app->session->setFlash(
                'success',
                Yii::t(
                    'user',
                    'Your experience have been updated.'
                )
            );
            return $this->redirect(['profile']);
        }

        return $this->render('updateexp', ['settings' => $settings]);
    }

view:

use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach ($settings as $index => $setting) {
 echo $form->field($setting, "[$index]skill_id")->label('skill');
echo $form->field($setting, "[$index]experience_year")->label('Experience');
}

ActiveForm::end();

yii2 Ajax Request Post


Html::a($service->name_en,'#', [
                         'title' => 'Ajax Title',
                         'onclick'=>"
                         $.ajax({
                        type     :'POST',
                        cache    : false,
                        data: {id: $service->id},
                        url  : '".Url::to(['site/cattt'])."',
                        success  : function(response) {
                                $('#showw').html(response);
                        },
                        error: function(){
                          console.log('failure');
                        }
                        });return false;",
                       ])

Friday, November 18, 2016

yii2 custom validator multiple attributes


public function rules()
    {
 [['is_property_owner', 'is_property_renant'], 'validateproperty'],

 }

    public  function validateproperty($attribute, $params){

        if(empty($this->is_property_owner) && empty($this->is_property_renant)){
            $this->addError('is_property_renant', 'Please select at least one option.');
        }


    }

yii2 custom validator multiple attributes


public function rules()
    {
 [['is_property_owner', 'is_property_renant'], 'validateproperty'],

 }

    public  function validateproperty($attribute, $params){

        if(empty($this->is_property_owner) && empty($this->is_property_renant)){
            $this->addError('is_property_renant', 'Please select at least one option.');
        }


    }

Wednesday, September 14, 2016

Yii2 Transaction


Transaction: A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query.
 $transaction = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }
OR
$transaction = $connection->beginTransaction();
try {
$user = $connection->createCommand()->insert('user', [
'name' => 'jibon',
'email' => 'jibon.bikash@gmail.com',
])->execute();
$connection->createCommand()->insert('user_role', ['role = "admin",
'user_id'=$user->id
])->execute();

$transaction->commit();

} catch(Exception $e) {
    $transaction->rollback();
}

Yii2 Transaction


Transaction: A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query.
 $transaction = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }
OR
$transaction = $connection->beginTransaction();
try {
$user = $connection->createCommand()->insert('user', [
'name' => 'jibon',
'email' => 'jibon.bikash@gmail.com',
])->execute();
$connection->createCommand()->insert('user_role', ['role = "admin",
'user_id'=$user->id
])->execute();

$transaction->commit();

} catch(Exception $e) {
    $transaction->rollback();
}

Yii2 Transaction


Transaction: A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query.
 $transaction = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }

Saturday, June 11, 2016

Yii2 Show image at a GridView

[
                'attribute' => 'itm_image',
                'format' => 'image',
                'value' => function($data) {
                    if($data->itm_image){
                        return \Yii::getAlias('@web/uploads/products/').$data->itm_image;
                    }
                    else{
                        return \Yii::getAlias('@web/uploads/products/no.png');
                    }
                },
                'contentOptions' => ['class' => 'item_image_grid img-responsive img-rounded']
            ],
            [
                'attribute' => 'itm_image',
                'format' => 'html',
                'value' => function ($data) {
                    if($data->itm_image) {
                        return Html::img('@web/uploads/products/' . $data->itm_image,
                            ['width' => '60px', 'class' => 'img-responsive img-rounded']);
                    }
                    else{
                        return Html::img('@web/uploads/products/no.png',
                            ['width' => '60px', 'class' => 'img-responsive img-rounded']);
                    }
                },
            ],

Sunday, June 5, 2016

yii2 file upload


if ($model->load(Yii::$app->request->post())){
            $image = UploadedFile::getInstance($model, 'image_file');
            $fileext=time();
            $image->saveAs('uploads/' . $fileext.$image->baseName . '.' . $image->extension);
            $model->image_file = $fileext.$image->baseName.'.'.$image->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }
        else{
            return $this->render('create', [
                'model' => $model,
            ]);
        }

Saturday, February 20, 2016

Yii2 change the value in GridView


            [
                'attribute' => 'active',
                'value' => function ($model) {
                    return $model->active == 1 ? 'Yes' : 'No';
                },
      //        'value' => function ($data){
       //             return $data->credit==1 ? "On": ($data->credit==2 ? "Pending": "Off");
        //        }

                'filter'=>array("1"=>"Yes","0"=>"No"),
          //      'filter' => Html::activeDropDownList($searchModel, 'attribute_name', ArrayHelper::map(ModelName::find()->asArray()->all(), 'ID', 'Name'),['class'=>'form-control','prompt' => '']),
            ],

Yii2 static dropDownList


$form->field($model, 'active')->dropDownList(['1' => 'Yes', '0' => 'No'],['prompt'=>'']);

yii2 beforeSave() method


    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->author = \Yii::$app->user->identity->id;
                $this->create_date = time();
            }
            else {
                $this->author=\Yii::$app->user->identity->id;
                $this->update_date=time();

            }

        }

        return true;
    }