Tags

, , ,

Tutorial ini memerlukan ekstensi mdmsoft/yii2-ar-behavior dan mdmsoft/yii2-widgets. Silakan install sesuai petunjuk yang ada di link.
Ok kita mulai. Misal kita punya tabel order dan tabel item yang saling berelasi.
Table Order:

Column Extra
id Primary
vendor_id
date
description

Table Item

Column Extra
order_id Primary
product_id Primary
qty
price

class Order extends ActiveRecord
{
    use \mdm\behavior\ar\RelationTrait;

    ....
    public function getItems()
    {
        return $this->hasMany(Item::className(),['order_id'=>'id']);
    }

    public function setItems($value)
    {
        $this->loadRelated('items', $value);
    }

}


class Item extends ActiveRecord
{
    ...
    public function getOrder()
    {
        return $this->hasOne(Order::className(),['id'=>'order_id']);
    }
}

Di controlernya kita buat kodenya seperti ini

public function actionCreate()
{
    $model = new Order();
    if($model->load(Yii::$app->request->post()){
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $model->items = Yii::$app->request->post('Item',[]);
            if($model->save()){
                $transaction->commit();
                return $this->redirect(['view','id'=>$model->id]);
            }else{
                $transaction->rollback();
            }
        }catch(Exception $e){
            $transaction->rollback();
            throw $e;
        }
    }
    return $this->render('create',['model'=>$model]);
}

Sedangkan di view-nya kita buat

// file _form.php

    <?php $form = ActiveForm::begin(); ?>
    <div class="box-body">
        field($model, 'vendor_id')->dropDownList(Vendor::selectOptions()); ?>

        field($model, 'date')->widget('yii\jui\DatePicker', [
                            'options' => ['class' => 'form-control', 'style' => 'width:50%'],
                                //'dateFormat' => 'php:d-m-Y',
                ]); ?>

        field($model, 'description')->textInput(['maxlength' => 64]) ?>

        <!-- Tabular Input -->
        <table class="tabular table-striped">
            <thead>
            <th class="col-lg-2">Product</th>
            <th class="col-lg-2">Qty</th>
            <th class="col-lg-2">@Price</th>
            <th class="col-lg-1"><a id="add-row" title="Add" href="#"><span class="glyphicon glyphicon-add"></span></a></th>
            </thead>
             'detail-grid',
                'allModels' => $model->items,
                'modelClass' => Item::className(),
                'options' => ['tag' => 'tbody'],
                'itemOptions' => ['tag' => 'tr'],
                'itemView' => '_item_detail',
                'clientOptions'=>[
                     'btnAddSelector'=>'#add-row',
                ]
            ])
            ?>
        </table>
    </div>
    <div class="box-footer">
        isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>
    <?php ActiveForm::end(); ?>

View untuk detail item

// file _item_detail.php

<td class="col-lg-2">
    <?= Html::activeTextInput($model, "[$key]product_id", ['class'=>'form-control','required' => true]) ?>
</td>
<td class="col-lg-2">
    <?= Html::activeTextInput($model, "[$key]qty", ['class'=>'form-control','required' => true]) ?>
</td>
<td class="col-lg-2">
    <?= Html::activeTextInput($model, "[$key]price", ['class'=>'form-control','required' => true]) ?>
</td>
<td  class="col-lg-1" style="text-align: center">
    <a data-action="delete" title="Delete" href="#"><span class="glyphicon glyphicon-trash"></span></a>
</td>

Silakan mencoba

Advertisements