FieldEditor.php
849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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()];
        }
    }
}