php生成yii model,【yii】giix工具扩展——自动生成model的属性翻译

giix是gii的扩展,方便实用。

GiixModel Generator用来生成model,和gii的Model Generator相比,giix把类拆分成两部分,base类和继承的model类。

base是用工具生成的,方便反复替换,而不会修改具体实现。

继承的类方便大家添加具体实现函数,而不用担心被工具重新替换掉。

好了,这下方便了,但是yii支持本地化操作,数据库的属性每次都需要手工翻译,略显麻烦,那我们就来改下giix的相关代码,让它自动生成对应的语言文件。

修改GiixModelCode.php里generateLabelsEx函数添加

在return $labels;

前添加// 保存语言文件

$langDir = Yii::getPathOfAlias('application.messages').DIRECTORY_SEPARATOR.Yii::app()->getLanguage().DIRECTORY_SEPARATOR;

if(!file_exists($langDir))

mkdir($langDir);

$messageFile = $langDir.'db.php';

if(file_exists($messageFile))

{

$messages=include($messageFile);

if(!is_array($messages))

$messages=array();

}else{

$messages=array();

}

foreach ($table->columns as $column)

{

if($column->comment)

$label=$column->comment;

else{

$label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?name)))));

$label = preg_replace('/\s+/', ' ', $label);

if (strcasecmp(substr($label, -3), ' id') === 0)

$label = substr($label, 0, -3);

if ($label === 'Id')

$label = 'ID';

}

$key = $table->name.'.'.$column->name;

if(!array_key_exists($key, $messages))

$messages[$key]=$label;

}

// 更新数据库翻译文件

$messageFileContent="<?php \n// 数据库数据字典\n\n\n\n";

$messageFileContent.="return array(\n";

foreach ($messages as $key => $value) {

$messageFileContent.=" '$key' => '$value',\n";

}

$messageFileContent.=');';

file_put_contents($messageFile, $messageFileContent);修改} else {

$label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?name)))));

$label = preg_replace('/\s+/', ' ', $label);

if (strcasecmp(substr($label, -3), ' id') === 0)

$label = substr($label, 0, -3);

if ($label === 'Id')

$label = 'ID';

$label = "Yii::t('app', '{$label}')";

}

修改为:} else {

// $label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?name)))));

// $label = preg_replace('/\s+/', ' ', $label);

// if (strcasecmp(substr($label, -3), ' id') === 0)

// $label = substr($label, 0, -3);

// if ($label === 'Id')

// $label = 'ID';

// $label = "Yii::t('app', '{$label}')";

$label = $table->name.'.'.$column->name;

$label = "Yii::t('db', '{$label}')";

}

注意:记得保证/protected/messages目录可写

效果,正常执行giix model Generator。然后就可以在messages下面的对应语言里面找到db.php,里面默认生成了对应的内容

2584779411c4ac1a3c0e3655fff6bbf5.png

修改对应的文字即可。

和giix之前生成的_basemodel的差别

c6367be7d4eb3314800dd6f75d989b9a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值