开发者

How to use MySQL FORMAT with CakePHP?

I cant figure out why when I try to use FORMAT function to limit number of decimal places in the results of MySQL query it doesn't work. Here is how my code looks like:

...some other options to join tables with some conditions....

$options['fields'] = array(
    'MetricSim.sim开发者_StackOverflow_id',
    'MetricSim.metric_id',
    'FORMAT(MetricSim.value,3) AS value'
);
$metrics_sims = $this->Sim->find('all', $options);

If I don't use the FORMAT function I get all of the results as expected. But when I try to use it I just don't get value field in my results (the rest of the fields are in place).


Why do you want to use FORMAT in your query? You can use a Helper to format your data in your view.

For example, in your controller class you add:

var $helpers = array('Number');

and in your view, you can format the value like:

$number->format($metric_sims['MetricSim']['value']);

(See NumberHelper class)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜