T
T
tolalur2018-02-17 13:07:25
Yii
tolalur, 2018-02-17 13:07:25

How to implement database query through ActiveDataProvider in yii2?

Good afternoon.
There is a model, a search model.
I did not figure out how to implement the query in terms of ActiveDataProvider , I had to use SqlDataProvider
I ask for advice on how to implement the query through ActiveDataProvider .
So far, the search model's search method looks like this:

public function search($params)
    {
        $this->load($params);

        $sql = 'SELECT r_s.Line_ID, r_s.Device_Type, r_s.Defect_ID, 
                           COUNT(r_s.Defect_ID) as count_kol, 
                           CONCAT("(", def.Code, ") ", def.Name) AS def_code_name
                        FROM Rejected_Stats r_s
                        LEFT JOIN Defect AS def ON r_s.Defect_ID = def.ID
                        WHERE `Time` >= :date_start AND `Time` <= :date_end'
                        . (!empty($this->Line_ID) ? ' AND Line_ID = :Line_ID' : '')
                        . (!empty($this->Device_Type) ? ' AND Device_Type = :Device_Type' : '')
                        . ' GROUP BY Line_ID, Device_Type, Defect_ID';

        $params = [':date_start' => $this->date_start,
                   ':date_end'   => empty($this->date_end) ? '9999-99-99' : $this->date_end
        ];

        if(!empty($this->Line_ID))
        {
            $params[':Line_ID'] = $this->Line_ID;
        }

        if(!empty($this->Device_Type))
        {
            $params[':Device_Type'] = $this->Device_Type;
        }

        $dataProvider = new SqlDataProvider([
            'db' => Yii::$app->dbSensors,
            'sql' => $sql,
            'params' => $params,
            'sort' => [
                'attributes' => [
                    'Line_ID',
                    'Device_Type',
                    'count_kol',
                    'def_code_name' => [
                        'asc'     => ['def.Name' => SORT_ASC],
                        'desc'    => ['def.Name' => SORT_DESC],
                        'default' => SORT_ASC
                    ],
                ],
                'defaultOrder' =>['count_kol' => SORT_DESC]
            ],
        ]);

        $dataProvider->pagination = false;

        return $dataProvider;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-02-17
@webinar

How to implement database query through ActiveDataProvider in yii2?

no way, ActiveDataProvider does not make queries to the database, there is AR for this.
Apparently we are talking about AR methods?
www.yiiframework.com/doc-2.0/guide-db-query-builde...
www.yiiframework.com/doc-2.0/guide-db-active-recor...
The links have examples with groupBy and joins, etc. .d. You form an activeRecords object and pass it to the ActiveDataProvider.
You write what exactly the problem is, and if you want someone to redo the code for you, then this is not here, but for freelance. What is the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question