Yii2: Infinite scrolling with GridView and ListView

This is one combo that I use frequently in my Yii2 projects. When the page is scrolled to the end, next set of data is retrieved and displayed automatically without the need to click the pagination link and without waiting for the page to load. This improves user experience.

Note that if you have huge amount of data, you still have to wait during the initial page load. But with infinite scrolling navigation through the any pages is smoother.

I’ve tried a few plugins but I find yii2-scroll-pager works the best with GridView and ListView widgets. This replaces the pagination links at the bottom of the page (or depends where you set it on the widget’s template).

The extension is installed via composer, or download here.

Here are the coding templates I used with these 2 widgets.

Continue »

MsSQL: Find Foreign Key References

Found via this source, this will list all foreign key relations in the database:

SELECT  obj.name AS FK_NAME,
    sch.name AS [schema_name],
    tab1.name AS [table],
    col1.name AS [column],
    tab2.name AS [referenced_table],
    col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
    ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
    ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
    ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
    ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
    ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
    ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

PHP: List Months Between 2 Dates

Based on code:

function list_months($start, $end) {
    $period = new DatePeriod(
        (new DateTime($start))->modify('first day of this month'),
        DateInterval::createFromDateString('1 month'),
        (new DateTime($end))->modify('first day of next month')
    );
    $month = [];
    foreach ($period as $dt) {
        $month[] = $dt->format('Ym');
    }
    return $month;
}

Notes:

  • Line 3: get the first of the month from start date.
  • Line 5: get the first of the end date’s next month. This to include month of end date in result.

Yii2: Authorization without RBAC

While RBAC allows you to define collection of permissions for your application, sometimes you just want something simpler to control access to certain pages. The simplest way to define your own access rule without using RBAC is to make use of $matchCallback property when definining Access Control rules.

In behaviors() function in Controller or Module:

Continue »