Model without DB table

Hello,

I need to put a view in my project, that will be only used to get report data from other tables, basically it just will be a small form in which you can salect one element from an existing model ("Programa") and a start and end date. I need to use a DropDownList and 2 DatePickers for this, and a submit button.

This has raised a few questions in me:

  1. Do I need to build a model or controller to achieve this?, and most important:

  2. How can I use dropdownlist, considering that it requires the $model parameter?

This is what I’ve done until now (according to this post)

Reporte model:




<?php


namespace app\models;


use yii\base\Model;


class ReporteForm extends Model

{

	public $programa;

	public $todosActivos;

	public $desde;

	public $hasta;


	public function rules()

	{

		return [

			[['programa', 'desde', 'hasta'], 'required'],

			[['programa'], 'exist', 'skipOnError' => true, 'targetClass' => Programa::className(), 'targetAttribute' => ['programa_id' => 'id_programa']],

		];

	}


	public function attributeLabels()

	{

		return [

			'programa' => 'Programa',

			'desde' => 'Desde',

			'hasta' => 'Hasta'

		];

	}

}



And this is my index, where I will select the parameters for the report:




<?php


use yii\helpers\Html;

use yii\helpers\ArrayHelper;

use yii\widgets\ActiveForm;

use kartik\date\DatePicker;

use kartik\widgets\Select2;

use app\models\Programa;


/* @var $this yii\web\View */


$this->title = 'Reporte de Insumos';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="reporte-index">


    <h1><?= Html::encode($this->title) ?></h1>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'programa')->widget(Select2::classname(), [

        'data' => ArrayHelper::map(Programa::find()->orderBy('programa')->all(), 'id_programa', 'programa'), 

        'options' => ['placeholder' => 'Seleccione Programa'],

        'pluginOptions' => [

            'allowClear' => true,

            ]

        ]

    ) ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


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


</div>



But this gives me an Undefined variable: model error

EDIT: Nevermind the Create, Update submit button, I have to remove that, and make the submit only runs a query.

Controller is required. Having a model is a good idea since you need to validate data submitted.

http://www.yiiframework.com/doc-2.0/guide-start-forms.html

Thank you :)

Follow up question: how can I use the submitButton to perform a specific action (the actual query in DB) in this controller? Let’s say “actionReport”, which will execute a query and return the result to another view.

Thanks.

Give it a value then check it in controller.