加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

cakephp – 从型号名称计算表名

发布时间:2020-12-13 21:42:48 所属栏目:PHP教程 来源:网络整理
导读:在我的Cake PHP应用程序中,我有一个这样的模型: class Duck extends AppModel { var $name = 'Duck'; function get_table_name() { $tbl_name = //compute default table name for this model }} 我想编写函数get_table_name(),它输出模型的默认表名.对于上
在我的Cake PHP应用程序中,我有一个这样的模型:

class Duck extends AppModel {
    var $name = 'Duck';

    function get_table_name() {
        $tbl_name = //compute default table name for this model         
    }
}

我想编写函数get_table_name(),它输出模型的默认表名.对于上面的例子,它应该输出ducks.

编辑:
有几个人指出使用$this->表.
我做了小测试,发现了以下内容:

>在我上面提到的问题中,$this->表确实包含表名.
>但是,实际上,我的代码看起来更像是这样的:

class Duck extends Bird {
    var $name = 'Duck';
    function get_table_name(){
        $tbl_name = //comput default table name for this model
    }
}
class Bird extends AppModel {

}

在这种情况下,$this->表是空字符串.
我采用这种方法是因为我想在两个模型之间共享一些代码.看起来这不是在需要一些常用功能的模型之间共享代码的好方法.

解决方法

要获取模型当前使用的表的名称,您可以使用:$this-> table.如果您不手动更改模型的表约定,那么在CakePHP不断更改其约定以使用除Inflector之外的其他内容的情况下,这可能是最有用的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读