Gracias.
Vista _form.php
div class=“contenido-admin”>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, ‘docto’)->dropDownList(
[
‘Factura Eléctronica’ => ‘Factura Eléctronica’,
‘Factura Exenta’ => ‘Factura Exenta’,
‘Factura Compra’ => ‘Factura Compra’,
‘Nota Crédito’ => ‘Nota Crédito’,
‘Nota Débito’ => ‘Nota Débito’,
]
) ?>
<?= $form->field($model, 'nro_docto')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'fecha')->textInput() ?>
<?= $form->field($model, ‘proveedor_id’)->dropDownList($model->listaProveedores,
[
‘prompt’ => ‘Seleccione un Proveedor…’
]
);
?>
<?php //= $form->field($model, 'proveedor_id')->textInput() ?>
<?= $form->field($model, 'neto')->textInput() ?>
<?= $form->field($model, 'iva')->textInput() ?>
<?= $form->field($model, 'total')->textInput() ?>
<?= Html::submitButton('Guardar', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
<?php
$js = <<registerJS($js);
?>
Modelo Compra.php
class Compra extends MyActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return ‘compra’;
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['docto', 'nro_docto', 'fecha', 'proveedor_id', 'neto', 'iva', 'total'], 'required'],
[['fecha', 'created_at', 'updated_at'], 'safe'],
[['proveedor_id', 'neto', 'iva', 'total', 'created_by', 'updated_by'], 'integer'],
[['docto'], 'string', 'max' => 30],
[['nro_docto'], 'string', 'max' => 12],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'docto' => 'Documento',
'nro_docto' => 'Nro',
'fecha' => 'Fecha',
'proveedor_id' => 'Proveedor',
'neto' => 'Neto',
'iva' => 'Iva',
'total' => 'Total',
'created_at' => 'Creado el',
'updated_at' => 'Editado el',
'created_by' => 'Creado por',
'updated_by' => 'Editado por',
];
}
public static function getListaProveedores()
{
$opciones = Proveedor::find()->asArray()->all();
return ArrayHelper::map($opciones, 'id', 'proveedor');
}
public function getProveedor()
{
return $this->hasOne(Proveedor::className(), ['id' => 'proveedor_id']);
}
public function getCreatedBy()
{
return $this->hasOne(Users::className(), ['id' => 'created_by']);
}
public function getUpdatedBy()
{
return $this->hasOne(Users::className(), ['id' => 'updated_by']);
}
}
y actionCreate() de controlador compraController.php
public function actionCreate()
{
$model = new Compra();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
Gracias de antemano.