orm: DATE functions

This commit is contained in:
yuri
2017-05-05 11:53:08 +03:00
parent 7add59ffcb
commit 6c287bc2e2
3 changed files with 44 additions and 4 deletions

View File

@@ -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";

View File

@@ -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=']);
}
}

View File

@@ -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);
}
}