День добрый.
Есть модель, модель поиска.
Не придумал как реализовать запрос в терминах
ActiveDataProvider, пришлось использовать
SqlDataProvider
Прошу совета как реализовать запрос именно через
ActiveDataProvider.
Пока метод
search модели поиска выглядит так:
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;