From: Coleman Watts Date: Thu, 5 May 2022 13:09:27 +0000 (-0400) Subject: APIv4 - Add MONTH sql function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=78afba9a8f3dd837d316c441c3e198e6544df715;p=civicrm-core.git APIv4 - Add MONTH sql function --- diff --git a/Civi/Api4/Query/SqlFunctionMONTH.php b/Civi/Api4/Query/SqlFunctionMONTH.php new file mode 100644 index 0000000000..4f903347a6 --- /dev/null +++ b/Civi/Api4/Query/SqlFunctionMONTH.php @@ -0,0 +1,46 @@ + 1, + 'optional' => FALSE, + ], + ]; + } + + /** + * @return string + */ + public static function getTitle(): string { + return ts('Month only'); + } + + /** + * @return string + */ + public static function getDescription(): string { + return ts('The numeric month (1-12) of a date.'); + } + +} diff --git a/tests/phpunit/api/v4/Action/SqlFunctionTest.php b/tests/phpunit/api/v4/Action/SqlFunctionTest.php index f56096fb84..9da481501c 100644 --- a/tests/phpunit/api/v4/Action/SqlFunctionTest.php +++ b/tests/phpunit/api/v4/Action/SqlFunctionTest.php @@ -265,7 +265,7 @@ class SqlFunctionTest extends UnitTestCase { $this->assertGreaterThanOrEqual($result[4]['rand'], $result[5]['rand']); } - public function testYearInWhereClause() { + public function testDateInWhereClause() { $lastName = uniqid(__FUNCTION__); $sampleData = [ ['first_name' => 'abc', 'last_name' => $lastName, 'birth_date' => '2009-11-11'], @@ -291,6 +291,14 @@ class SqlFunctionTest extends UnitTestCase { ->selectRowCount() ->execute(); $this->assertCount(2, $result); + + // Try an expression in the value + $result = Contact::get(FALSE) + ->addWhere('last_name', '=', $lastName) + ->addWhere('MONTH(birth_date)', '=', 'MONTH("2030-11-12")', TRUE) + ->addSelect('birth_date') + ->execute()->single(); + $this->assertEquals('2009-11-11', $result['birth_date']); } }