Welcome Guest, Not a member yet? Register   Sign In
Using functions in selects with DataMapper OZ
#1

[eluser]macron[/eluser]
My problem is that I want to format the ISO date used by MySQL using the DATE_FORMAT mysql function. I'm aware of the ActiveRecord bug as described in the DataMapper documentation but this does not work:
Code:
$n->new News();
$n->select("id,headline,dk,DATE_FORMAT(date,'%e %M %Y')")->order_by('date', 'desc')->limit(3)->get();
Why?
I wan't something like this to come out of the query:
Code:
SELECT `id`, `headline`, `dk`, DATE_FORMAT(date, '%e %M %Y') FROM `news` ORDER BY `news`.`date` desc LIMIT 3

What am I doing wrong here?
#2

[eluser]macron[/eluser]
Argh. Sorry for the newbee q. here. Found the answer: adding a FALSE as a second param. to make the select not quote the mysql function
#3

[eluser]WanWizard[/eluser]
See the manual on how to deal with functions in Datamapper:
Code:
$n->select("id,headline,dk")->select_func("DATE_FORMAT", array('@date', '[,]', "'%e %M %Y'"), 'date')->order_by('date', 'desc')->get();
For security reasons, you don't want to disable escaping if other options are available.
#4

[eluser]macron[/eluser]
Thanks.
Whats the '[,]' in the array?
#5

[eluser]WanWizard[/eluser]
Have you looked at the manual (I've included the link)? The third bullet at the top says it all...
#6

[eluser]macron[/eluser]
Hi
Yes. I get it. It's a raw string. But why is the formatting string (%e...) not in square brackets or the comma not in double-quotes. Why the diff.?
#7

[eluser]WanWizard[/eluser]
The formatting string is a string, enclosed in quotes. Anything enclosed in quotes is passed without escaping, so the brackets aren't needed. The comma can't be enclosed in quotes, yet you need to prevent it from being escaped.




Theme © iAndrew 2016 - Forum software by © MyBB