Merge pull request #363 from tractorcow/pulls/blog-datefilter

BUG Fix blog date filtering for additional database server types
This commit is contained in:
Daniel Hensby 2016-01-25 10:34:39 +00:00
commit 80cb06f715
4 changed files with 14 additions and 28 deletions

View File

@ -35,7 +35,7 @@ matrix:
- php: 5.6 - php: 5.6
env: DB=PGSQL CORE_RELEASE=3.2 env: DB=PGSQL CORE_RELEASE=3.2
- php: 5.4 - php: 5.4
env: DB=MYSQL env: DB=SQLITE
- php: 5.3 - php: 5.3
env: DB=MYSQL env: DB=MYSQL
- php: hhvm - php: hhvm

View File

@ -473,35 +473,21 @@ class Blog extends Page implements PermissionProvider
$query->innerJoin('BlogPost', sprintf('"SiteTree%s"."ID" = "BlogPost%s"."ID"', $stage, $stage)); $query->innerJoin('BlogPost', sprintf('"SiteTree%s"."ID" = "BlogPost%s"."ID"', $stage, $stage));
// getConn is deprecated, but not get_conn in 3.1 $conn = DB::getConn();
$getConnectionMethod = 'getConn';
if (method_exists('DB','get_conn')) {
$getConnectionMethod = 'get_conn';
};
// Filter by year
$yearCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%Y');
$query->where(sprintf('%s = \'%04d\'', $yearCond, Convert::raw2sql($year)));
if (DB::$getConnectionMethod() instanceof MySQLDatabase) { // Filter by month (if given)
$query->where(sprintf('YEAR("PublishDate") = \'%s\'', Convert::raw2sql($year))); if ($month) {
$monthCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%m');
$query->where(sprintf('%s = \'%02d\'', $monthCond, Convert::raw2sql($month)));
if ($month) { if ($day) {
$query->where(sprintf('MONTH("PublishDate") = \'%s\'', Convert::raw2sql($month))); $dayCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%d');
$query->where(sprintf('%s = \'%02d\'', $dayCond, Convert::raw2sql($day)));
if ($day) {
$query->where(sprintf('DAY("PublishDate") = \'%s\'', Convert::raw2sql($day)));
}
} }
} elseif (DB::$getConnectionMethod() instanceof PostgreSQLDatabase) {
$where = sprintf('EXTRACT(YEAR FROM "PublishDate") = \'%s\'', Convert::raw2sql($year));
if ($month) {
$where .= sprintf(' AND EXTRACT(MONTH FROM "PublishDate") = \'%s\'', Convert::raw2sql($month));
if ($day) {
$where .= sprintf(' AND EXTRACT(DAY FROM "PublishDate") = \'%s\'', Convert::raw2sql($day));
}
}
$query->where($where);
} }

View File

@ -8,7 +8,7 @@
], ],
"type": "silverstripe-module", "type": "silverstripe-module",
"require": { "require": {
"silverstripe/cms": ">=3.1.0", "silverstripe/cms": "^3.1.0",
"silverstripe/lumberjack": "~1.1", "silverstripe/lumberjack": "~1.1",
"silverstripe/tagfield": "^1.0" "silverstripe/tagfield": "^1.0"
}, },

View File

@ -7,7 +7,7 @@ Group:
Title: Editors Title: Editors
BlogUsers: BlogUsers:
Title: Blog Users Title: Blog Users
Code: BlogUsers Code: blogusers
Permission: Permission:
Administrators: Administrators: