diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index edffcaf949..795f39513f 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -211,6 +211,15 @@ abstract class Base return "DATE_FORMAT({$part}, '%Y-%m')"; case 'DAY': return "DATE_FORMAT({$part}, '%Y-%m-%d')"; + case 'MONTH_NUMBER': + $function = 'MONTH'; + break; + case 'DATE_NUMBER': + $function = 'DATE'; + break; + case 'YEAR_NUMBER': + $function = 'YEAR'; + break; } if ($distinct) { $idPart = $this->toDb($entityName) . ".id"; diff --git a/tests/unit/Espo/Core/SelectManagers/BaseTest.php b/tests/unit/Espo/Core/SelectManagers/BaseTest.php index 886a47127b..d35628dfc5 100644 --- a/tests/unit/Espo/Core/SelectManagers/BaseTest.php +++ b/tests/unit/Espo/Core/SelectManagers/BaseTest.php @@ -256,12 +256,12 @@ class BaseTest extends \PHPUnit_Framework_TestCase 'value' => array( array( 'type' => 'equals', - 'field'=> 'date', + 'attribute'=> 'date', 'value' => '2016-10-10' ), array( 'type' => 'after', - 'field'=> 'dateTime', + 'attribute'=> 'dateTime', 'value' => '2016-10-10 10:10:00' ) ) @@ -273,4 +273,23 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('2016-10-10', $selectParams['whereClause'][0]['NOT'][0]['date=']); } + + function testBuildSelectParamsComplex() + { + $selectManager = new \Espo\Core\SelectManagers\Base($this->entityManager, $this->user, $this->acl, $this->aclManager, $this->metadata, $this->config); + $selectManager->setEntityType('Test2'); + + $params = array( + 'where' => array( + array( + 'type' => 'equals', + 'attribute'=> 'MONTH_NUMBER:dateTime', + 'value' => 2 + ) + ) + ); + + $selectParams = $selectManager->buildSelectParams($params); + $this->assertEquals(2, $selectParams['whereClause'][0]['MONTH_NUMBER:dateTime=']); + } } diff --git a/tests/unit/Espo/ORM/DB/QueryTest.php b/tests/unit/Espo/ORM/DB/QueryTest.php index ba9b00c3a2..95388c7f45 100644 --- a/tests/unit/Espo/ORM/DB/QueryTest.php +++ b/tests/unit/Espo/ORM/DB/QueryTest.php @@ -392,6 +392,18 @@ class QueryTest extends PHPUnit_Framework_TestCase $this->assertEquals($expectedSql, $sql); } -} - + public function testFunction1() + { + $sql = $this->query->createSelectQuery('Comment', array( + 'select' => ['id'], + 'whereClause' => array( + 'MONTH_NUMBER:comment.created_at' => 2 + ) + )); + $expectedSql = + "SELECT comment.id AS `id` FROM `comment` " . + "WHERE MONTH(comment.created_at) = '2' AND comment.deleted = '0'"; + $this->assertEquals($expectedSql, $sql); + } +} \ No newline at end of file