CakePHP自定义全局函数 [英] CakePHP Custom Global Function

查看:128
本文介绍了CakePHP自定义全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的应用程序有几个数据库字段称为活动,它是布尔的。但是,我不想在视图中显示1或0,而是Yes或No。

The application I am working on have several database fields called "active", which is boolean. However, instead of displaying "1" or "0" in views, I would like it to say "Yes" or "No".

我有以下功能:

function activeFriendlyName($status)
    {
    	if ($status == 1)
    	{
    		return "Yes";
    	}
    	else
    	{
    		return "No";
    	}
    }

但是,我不确定应该把这个全局功能?它会是app_model.php文件?此外,我如何调用此函数应用格式化?

However, I am unsure where I should put this global function? Would it be the app_model.php file? In addition, how would I call this function to apply the "formatting"?

推荐答案

直到你需要显示它。这意味着视图是更改它的正确的地方。我只需要一个简单的:

You should leave the data coming from the database as is until you need to display it. That means the View is the right place to change it. I'd just go with a simple:

echo $model['Model']['bool'] ? "Yes" : "No";

但是如果你需要更复杂的形式规则,你不想每次都重复,

But if you need more complex formating rules that you don't want to repeat every time, make a custom Helper.

可以 bootstrap.php 中定义一个全局函数,我不会推荐它。

You could define a global function in bootstrap.php, but I wouldn't recommend it.

这篇关于CakePHP自定义全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆