Nextras\ORM vypsání dat podle roku

TOMeek
Člen | 64
+
0
-

Zdravím,
koketuji s Nextras ORM a narazil jsem na problém, který jsem nebyl schopen dogooglit.
V Nette database mám

items->findBy( ["YEAR(created)" => Date( "Y" )] )

A vše je OK. Ale když to samé dám do ORM, tak to skončí chybou Unsupported condition format.
Zkoušel jsem i zápis YEAR([created]), ale nepomohlo to.
Nemáte někdo radu, jak vypsat data za calý rok, když pole je ve formátu datetime? (abych nemusel dělat nějaký strašný zápis od-do)

Editoval TOMeek (5. 1. 2018 16:58)

hrach
Člen | 1834
+
+1
-

@TOMeek cau, na Repository vrstve nelze takto naprimo pouzivat MySQL (obecne DB) funkce. Musis si napsat vlastni query dotaz na mapper vrstve nebo si napsat vlastni custom funkci (novinka v 3.0). Do budoucna planuji repo s vlasntimi funkcemi, ktere by zrovna toto uz umelo.

Custom funkce pro ORM 3.0 muze vypadat cca takto:

class YearFunction implements
	Nextras\Orm\Mapper\Dbal\CustomFunctions\IQueryBuilderFilterFunction,
	Nextras\Orm\Mapper\Memory\CustomFunctions\IArrayFilterFunction
{
    public function processArrayFilter(ArrayCollectionHelper $helper, IEntity $entity, array $args): bool
	{
		assert(count($args) === 2 && is_string($args[0]) && is_int($args[1]));
		$value = $helper->getValue($entity, $args[0])->value;
		return (int) $value->format('Y') === args[1];
	}

    public function processQueryBuilderFilter(QueryBuilderHelper $helper, QueryBuilder $builder, array $args): array
	{
		assert(count($args) === 2 && is_string($args[0]) && is_int(args[1]));
		// convert expression to column name (also this autojoins needed tables)
		$column = $helper->processPropertyExpr($builder, $args[0])->column;
		return ['YEAR(%column) = %i', $column, $args[1]];
	}
}

K tomu viz dokumentace: https://nextras.org/…on-functions (+ jeste zaregistrovat custom funkci)

Pouziti pak jednoduse:

$collection->findBy([YearFunction::class, 'created', (int) date('Y')]);
TOMeek
Člen | 64
+
0
-

@hrach Děkuji mnohokrát. Udělal jsem to tou custom funkcí a funguje to náramně.