我正在尝试整理一个下拉字段,但我不确定我是否采取了正确的方法。我有一个模型,它的字段"type"是一个下拉列表,显示一个字符串,但存储一个整数。为了格式化输出,我将Grid子类化为
class Model_Member extends MVCTable {
function init() {
parent::init();
$this->addField('type')
->type('list')
->display(array('grid'=>'mbrtype'))
->caption('Member Type')
->listData(array('Guest','Associate','Full'))
;
}
}
为了格式化输出,我将Grid子类化为
class Grid extends Grid_Basic {
function format_mbrtype($field) {
$mt=array('Guest','Associate','Full');
$this->current_row[$field]=$mt[$this->current_row[$field]];
}
}
当我加载我的管理员CRUD的成员记录时,我看到这个字段填充了数字,而不是格式化的数据。我曾期望控制器获取传递到display()中的值,并执行我的格式化程序,而不是使用标准的格式化程序。
我是不是遗漏了什么?我浏览了一下源代码,我对出现问题的地方的最佳猜测是在MVCGrid.php的第45行。对Controller对象的formatType()的调用不包括作为第三个参数的字段名,这会导致它忽略字段的显示集合的映射,并返回到Controller的$type_corresponsence数组。
当然,我使用ATK4才几周,所以我可能只是把事情联系在一起了。这是实现自定义格式化程序的正确方法吗?
ATK 4.1.3中存在问题:
实际上有一个问题。你需要修补atk4插件:
diff --git a/mvc/MVCGrid.php b/mvc/MVCGrid.php
index 90bd365..0fb0c0b 100644
--- a/mvc/MVCGrid.php
+++ b/mvc/MVCGrid.php
@@ -42,7 +42,7 @@ class MVCGrid extends Grid{
$field=$this->getController()->getModel()->getField($field_name);
if(is_null($field))throw new Exception_InitError("Field '$field_name' is not defined in the ".
get_class($this->getController()->getModel())." model");
- if(is_null($type))$type=$this->getController()->formatType($field->datatype(),'grid');
+ if(is_null($type))$type=$this->getController()->formatType($field->datatype(),'grid',$field_name);
if($field_name=='locked')return
parent::addColumn('locked','locked','');
if($field_name=='id')$type='text';
或者打开atk4 addons/mvc/MVCGrid.php,在第45行,将第3个参数添加到formatType(…..,$field_name)的调用中;
这应该行得通。
或者
在github中将atk4-addons升级到"master",github仍在4.1分支上。
演示:http://codepad.agiletoolkit.org/myformat
定义"显示"字段的正确方法是通过数组('grid'=>'myfield');
感谢您注意到这个问题!