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'=>'']);