Answer the question
In order to leave comments, you need to log in
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
How to implement database query through ActiveDataProvider in yii2?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question