Skip to content

Instantly share code, notes, and snippets.

@opotemkin
Created June 9, 2017 12:48
Show Gist options
  • Save opotemkin/1e62a378283aa2f50f34a7a205f73c48 to your computer and use it in GitHub Desktop.
Save opotemkin/1e62a378283aa2f50f34a7a205f73c48 to your computer and use it in GitHub Desktop.
create SqlDataProvider in the Yii2
// тек.время по дефолт.таймзоне с временем блокировки
$current_time = (new \DateTime())
->setTimezone(new \DateTimeZone(Timezone::DEFAULT_TIMEZONE))
->getTimestamp();
// Время через час
$current_time_hour = $current_time + 60*60;
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT "queue".*, COUNT(DISTINCT "order1".id) as count, COUNT(DISTINCT "order2".id) as count_hour
FROM "queue"
LEFT JOIN "order" as "order1" ON "queue".id="order1".last_queue_id
LEFT JOIN "order" as "order2" ON "queue".id="order2".last_queue_id
WHERE (
("order1"."blocked_to"<=:current_time AND "order1"."blocked_to" IS NOT NULL)
OR "order1"."blocked_to" IS NULL)
AND (
("order2"."blocked_to"<=:current_time_hour AND "order2"."blocked_to" IS NOT NULL)
OR "order2"."blocked_to" IS NULL)
AND "queue".active=:active
GROUP BY "queue".id',
'params' => [
':active' => 1,
':current_time' => $current_time,
':current_time_hour' => $current_time_hour,
],
// сортировка
'sort' => [
'attributes' => [
'id' => [
'asc' => ['id' => SORT_ASC], // от А до Я
'desc' => ['id' => SORT_DESC], // от Я до А
'default' => SORT_DESC, // сортировка по умолчанию
],
'priority' => [
'asc' => ['priority' => SORT_ASC],
'desc' => ['priority' => SORT_DESC],
'default' => SORT_DESC,
],
'priority_selection' => [
'asc' => ['priority_selection' => SORT_ASC],
'desc' => ['priority_selection' => SORT_DESC],
'default' => SORT_DESC,
],
'count' => [
'asc' => ['count' => SORT_ASC],
'desc' => ['count' => SORT_DESC],
'default' => SORT_DESC,
],
],
],
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment