FieldEditor.php 849 Bytes
<?php
namespace backend\components\FieldEditor;
use common\models\DetailsCriteria;
use yii\base\Widget;


class FieldEditor extends Widget
{
    public $name;
    public $brand;
    public $model;
    public $template;

    public function init(){

        parent::init();

    }


    public function run()
    {
        if($this->name && $this->brand && $this->model){
            $widgetData = $this->findModel();
        } else {
            $widgetData= [new DetailsCriteria()];
        }


        return $this->render($this->template.'_field',['model'=>$widgetData]);
    }

    protected function findModel()
    {
        if (($model = DetailsCriteria::find()->where(['name'=>$this->name, 'brand'=>$this->brand])->all()) !== null) {

            return $model;

        } else {
            return [new DetailsCriteria()];
        }
    }
}