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

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:
$n->new News();
$n->select("id,headline,dk,DATE_FORMAT(date,'%e %M %Y')")->order_by('date', 'desc')->limit(3)->get();
I wan't something like this to come out of the query:
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?

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

See the manual on how to deal with functions in Datamapper:
$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.

Whats the '[,]' in the array?

Have you looked at the manual (I've included the link)? The third bullet at the top says it all...

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.?

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