Search code examples
yii2

Why doesn't the Yii2 filter and search work?


I'm new to Yii2 and have just started learning. I have 2 tables Task and Clients. Created the Myprolognation filtering and search model, which outputs data from the Task and Clients tables. Filtering and searching are not possible. Code:

View Myprolognation:

<?php

use yii\helpers\Html;
use yii\data\ActiveDataProvider;
use yii\grid\GridView;
use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;
use kartik\date\DatePicker;
use kartik\datetime\DateTimePicker;
use yii\db\ActiveRecord;
use yii\model\Myprolognation;
use app\models\Task;
use app\from\models\Taskreport;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use budyaga\users\models\User;
use app\models\Tasktypes;
use app\models\Tasktypeshablon;

$this->title = 'Мои продления';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="panel panel-default" >
    <div class="panel-heading">
        <h3 class="panel-title row">
            <div class="col-lg-9">
            Мои продления
            </div>
        </h3>
    </div>
    <div class="panel-body collapse in" id="taskpanel">
        <?php
        $dataProvider = new ActiveDataProvider(['query' => $query]);
        echo 'Всего задач '.$query->count();
        echo GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'summary'=>'',
            'columns' => [
                [
                    'attribute'=>'number',
                    'format' => 'raw',
                    'value'=>function ($data) {
                        $res= $data['number'];
                        return $res;
                    },
                ],
                [
                    'attribute'=>'clientname',
                    'format' => 'raw',
                    'value'=>function ($data) {
                        $res= Html::a($data['clientname'],['clients/show','client_id'=>$data['client_id']],['data-pjax' => '0','target'=>'_blank']);
                        $rest=mb_substr($res,0,100);
                        return $rest;
                    },
                ],
                [
                'attribute'=>'name',
                    'format' => 'raw',
                    'value'=>function ($data) {
                    $res=  $data['name'];
                        return $res;
                    },
                ],
            
                [
                    'attribute'=>'deadline',
                    'format' => ['date', 'php:d.m.Y'],
                    'value'=>function ($data) {
                    $res=  $data['deadline'];
                    $res=date("d.m.Y", strtotime($res));
                        return $res;
                    },
                ],
                [
                    'attribute'=>'accounting_status',
                    'format' => 'raw',
                    'value' => function ($data) {                           
                        $res= $data['accounting_status'];
                        return Html::dropDownList('accounting_status', $res,[''=>'','В работе'=>'В работе','Продлено'=>'Продлено','Удержание'=>'Удержание','Работает ОП'=>'Работает ОП','Закрыт'=>'Закрыт','Убрана из работы'=>'Убрана из работы'],['prompt' => $res]);
                    },
                ],
                [
                'attribute'=>'accounting_plan_directions',
                'format' => 'raw',                  
                'headerOptions' => ['style' => 'width:6%'],
                'value'=>function ($data) {
                    $res=  $data['accounting_plan_directions'];
                    return Html::a($res,'#id',['id'=>$data['accounting_plan_directions'],'style' => 'width:100%', 'class'=>'btn btn-secondary dropdown-toggle', 'data-toggle'=>'modal', 'data-placement'=>'right']); 
                }, 
                ],
                [
                'attribute'=>'accounting_plan_sum',
                    'format' => 'raw',
                    'headerOptions' => ['style' => 'width:6%'],
                    'value'=>function ($data) {
                    $res=  $data['accounting_plan_sum'];
                    return Html::textInput('', $res,['style' => 'width:100%']);
                    },
                ],
                [
                'attribute'=>'accounting_date_pay',
                'format' => 'raw', 
                'headerOptions' => ['style' => 'width:6%'],
                'value' => function ($data) {
                    $res = isset($data['accounting_date_pay']) ? $data['accounting_date_pay'] : '';
                    $formattedDate = !empty($res) ? date('d.m.Y', strtotime($res)) : '';
                    return DatePicker::widget([
                        'name' => 'accounting_date_pay',
                        'value' => $formattedDate,
                        'convertFormat' => true,
                        'type' => DateTimePicker::TYPE_INPUT,
                        'pluginOptions' => [
                            'format' => 'dd.MM.yyyy',
                            'todayHighlight' => true,
                        
                        ],
                    ]);
                },
                ],
                [
                'attribute'=>'accounting_fact_sum',
                    'format' => 'raw',
                    'headerOptions' => ['style' => 'width:6%'],
                    'value'=>function ($data) {
                    $res=  $data['accounting_fact_sum'];
                    return Html::textInput('', $res,['style' => 'width:100%']);
                    },
                ],
                [
                'attribute'=>'accounting_retention_sum',
                    'format' => 'raw',
                    'headerOptions' => ['style' => 'width:6%'],
                    'value'=>function ($data) {
                    $res=  $data['accounting_retention_sum'];
                    return Html::textInput('', $res,['style' => 'width:100%']);
                    },
                ],
                [
                'attribute'=>'accounting_fact_directions',
                    'format' => 'raw',
                    'headerOptions' => ['style' => 'width:6%'],
                    'value'=>function ($data) {
                    $res=  $data['accounting_fact_directions'];
                    return Html::textInput('', $res,['style' => 'width:100%']);
                    },
                ],
                [
                'attribute'=>'accounting_date_broadcast',
                'format' => 'raw', 
                'headerOptions' => ['style' => 'width:6%'],
                'value' => function ($data) {
                    $res = isset($data['accounting_date_broadcast']) ? $data['accounting_date_broadcast'] : '';
                    $formattedDate = !empty($res) ? date('d.m.Y', strtotime($res)) : '';
                    return DatePicker::widget([
                        'name' => 'accounting_date_broadcast',
                        'value' => $formattedDate,
                        'convertFormat' => true,
                        'type' => DateTimePicker::TYPE_INPUT,
                        'pluginOptions' => [
                            'format' => 'dd.MM.yyyy',
                            'todayHighlight' => true,
                        ],
                    ]);
                },
                ],
                [
                'attribute'=>'accounting_last_invoice',
                    'format' => 'raw',                  
                    'headerOptions' => ['style' => 'width:6%'],
                    'value'=>function ($data) {
                    $res=  $data['accounting_last_invoice'];
                    return Html::textInput('', $res,['style' => 'width:100%']);
                    },
                ],
                [
                'attribute'=>'accounting_type_retention',
                    'format' => 'raw',
                    'value'=>function ($data) {
                    $res=  $data['accounting_type_retention'];
                        return Html::dropDownList('accounting_type_retention', $res,[''=>'','Закрытие'=>'Закрытие','Не требуется'=>'Не требуется','Нет контактов'=>'Нет контактов','Риск отвала'=>'Риск отвала','Задержка оплаты'=>'Задержка оплаты','Просят скидку'=>'Просят скидку','Деление счета'=>'Деление счета','Конкуренты'=>'Конкуренты'],['prompt' => $res]);
                    },
                ],
            ],
        ]); 
        ?>
    </div>
</div>


Model Myprolognation:

<?php

namespace app\models\form;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
use app\models\Task;
use app\models\Clients;
use app\models\form\Taskreport;
use app\models\form\Myprolognation;

class Myprolognation extends ActiveRecord
{
    public $number;
    public $clientname;
    public $name;
    public $deadline;
    public $accounting_status;
    public $accounting_plan_directions;
    public $accounting_plan_sum;
    public $accounting_date_pay;
    public $accounting_fact_sum;
    public $accounting_retention_sum;
    public $accounting_fact_directions;
    public $accounting_date_broadcast;
    public $accounting_last_invoice;
    public $accounting_type_retention;

    public function getClients()
    {
        return $this->hasMany(Clients::class, ['client_id' => 'id']);
    }

    public function getTask()
    {
        return $this->hasMany(Task::class, ['task_id' => 'id']);
    }

    public function rules(){
        return [
            [['number','clientname', 'name','deadline', 'accounting_status', 'accounting_plan_directions', 'accounting_plan_sum', 'accounting_date_pay', 'accounting_fact_sum', 'accounting_retention_sum', 'accounting_fact_directions', 'accounting_date_broadcast', 'accounting_last_invoice', 'accounting_type_retention'], 'string','max' => 20],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }
    
    public function search($params)
    {
        $query = Task::find()->joinWith(['clients']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['attributes' => ['number','clientname', 'name','deadline', 'accounting_status', 'accounting_plan_directions', 'accounting_plan_sum', 'accounting_date_pay', 'accounting_fact_sum', 'accounting_retention_sum', 'accounting_fact_directions', 'accounting_date_broadcast', 'accounting_last_invoice', 'accounting_type_retention']]
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'number' => $this->number,
            'clientname' => $this->clientname,
            'name' => $this->name,
        ]);

        
        $query->andFilterWhere(['like', 'deadline', $this->deadline])
            ->andFilterWhere(['like', 'accounting_status', $this->accounting_status])
            ->andFilterWhere(['like', 'accounting_plan_directions', $this->accounting_plan_directions])
            ->andFilterWhere(['like', 'accounting_plan_sum', $this->accounting_plan_sum])
            ->andFilterWhere(['like', 'accounting_date_pay', $this->accounting_date_pay])
            ->andFilterWhere(['like', 'accounting_fact_sum', $this->accounting_fact_sum])
            ->andFilterWhere(['like', 'accounting_retention_sum', $this->accounting_retention_sum])
            ->andFilterWhere(['like', 'accounting_fact_directions', $this->accounting_fact_directions])
            ->andFilterWhere(['like', 'accounting_date_broadcast', $this->accounting_date_broadcast])
            ->andFilterWhere(['like', 'accounting_last_invoice', $this->accounting_last_invoice])
            ->andFilterWhere(['like', 'accounting_type_retention', $this->accounting_type_retention]); 

        return $dataProvider;
    } 

    public function attributeLabels()
    {
        return array(
            'number' => 'Карта',
            'clientname' => 'Клиент',
            'name' => 'Тип задачи',
            'deadline' => 'Дедлайн',
            'accounting_status'=> 'Статус',
            'accounting_plan_directions'=> 'Плановое количество направлений продления',
            'accounting_plan_sum'=> 'Плановая сумма оплаты',
            'accounting_date_pay'=> 'Дата оплаты',
            'accounting_fact_sum'=> 'Сумма поступившей оплаты',
            'accounting_retention_sum'=> 'Сумма оплаты по удержанию',
            'accounting_fact_directions'=> 'Фактическое количество продленных направлений',
            'accounting_date_broadcast'=> 'Дата передачи',
            'accounting_last_invoice'=> 'Сумма последнего отправленного счета',
            'accounting_type_retention'=> 'Тип удержания',

        );
    }
}
?>

TaskController:

public function actionMyprolognation()
    {   
        $searchModel = new Myprolognation();
        $dataProvider = new ActiveDataProvider([
            'query' => $searchModel->search(Yii::$app->request->queryParams),
        ]); 

        $trep = new Taskreport();   
        $trep->task_type_shablon_id="706576";
        $trep->setDefault(); 

        

        return $this->render('myprolognation', ['query' => TaskController::getReportquery($trep), 'model' => $trep, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);

    }

enter image description hereenter image description hereenter image description here


Solution

  • use app\from\models\Taskreport; = > use app\models\form\Taskreport;