我是 Agiletoolkit 的新手,仍在学习中。这是我的问题:
我有一个用于管理工作的常规 CRUD。
$this->add('CRUD')->setModel('Job');
模型包含字段“job_number”,应在“添加”页面上自动填写:N762、N763 等。
这是可以在模型或从 CRUD 表调用的 addjob 页面中完成的事情吗?
谁能给我一些非常简单的代码示例?
谢谢
我相信这应该在模型级别完成。它基本上与视图级别无关。
如果这个 job_number 字段与 ID 严格相关,那么您甚至可以将其作为模型中的表达式字段,甚至不将其存储在数据库中。例如,job_number = id + 100。
但是如果你真的想将它存储在数据库中,那么你应该这样做:
1) 在模型中将 job_number 创建为具有类型('number')的普通字段,但使用 ->system(true) 或 ->editable(false),具体取决于您希望在何处查看此字段(表单、网格);
2) 在模型初始化方法中添加钩子 afterInsert。
function init(){
parent::init();
// ... your field definitions here
// add afterInsert hook
$this->addHook('afterInsert',array($this,'afterInsert'));
}
3)在模型创建方法中
function afterInsert($m,$new_id){ // <-- new_id is ID of newly inserted record
$m->load($new_id);
$m['job_number'] = $new_id + 100; // <-- your function here
$m->save();
}
or maybe you can even write it simpler - not sure
function afterInsert($m,$new_id){ // <-- new_id is ID of newly inserted record
$this->set('job_number',$new_id + 100); // <-- your function here
$this->save();
}
这应该有效,但我没有对此进行测试。只是在飞行中写在这里。
试试这个,如果您需要更多帮助,请告诉我们。此外,非常欢迎您加入 ATK4 IRC 频道并直接提问。
这样的事情应该解决你的问题:
在模型初始化中:
$this->addField("job_no")
->defaultValue("N" . $this->dsql()->del("fields")->field(
$this->dsql()->expr("max(id) + 1"), "nr"
)->getOne());
不过没有测试。