1
我有一个非常简单的类这样做,只是为了学习,但我不能把它的工作就像我在http://agiletoolkit.org/learn/understand/model/actions更改值挂钩
这是类定义见:
class Model_Task extends Model_Table {
public $table='task';
function init(){
parent::init();
$this->addField('user_id')->system(true);
$this->addField('name')->mandatory('No has indicado un nombre para la tarea');
$this->addField('description')->dataType('text');
$this->addField('state')->system(true);
$this->addHook('beforeSave',function($m){
$m->description='test';
return $m;
});
$this->debug();
}
}
我试图whih的samble页面格式太:
class Model_Task extends Model_Table {
public $table='task';
function init(){
parent::init();
$this->addField('user_id')->system(true);
$this->addField('name')->mandatory('No has indicado un nombre para la tarea');
$this->addField('description')->dataType('text');
$this->addField('state')->system(true);
$this->addHook('beforeSave',$this);
$this->debug();
}
function BeforeSave(){
$this->description='test';
return $this;
}
}
任务测试页也很简单:
class page_Task extends Page {
function init(){
parent::init();
$m=$this->add('Model_Task');
$f=$this->add('Form');
$f->setModel($m);
$f->addSubmit('Guardar');
//Task submit
$f->onSubmit(function($form){
$form->update();
$form->js()->univ()->redirect('index?add_ok=1')->execute();
});
}
}
在模型描述的两个实现中,都以表格中插入的值保存,而不是使用'Test'。如果我在beforeTest函数中回显$ this-> description或$ m-> description,那么在我设置它之后和'Test'之后它是空的,但它不会使生成的sql无效。当然,我想说一些问题,但是 - 什么?
谢谢!
感谢罗马!我以这种方式开始并改变为 - >以查看它是否有效,并且我用thas语法发送了我的示例,但是我之前检查了$ this ['description']。 – Jaume
谢谢罗马!我以这种方式开始并改变为 - >以查看它是否有效,并且我用thas语法发送了我的示例,但是我之前检查了$ this ['description']。现在我再次检查了它,并且看到它在beforeSave中工作,但不在BeforeInsert中。在beforeInsert中启动了钩子,但我在调试模式下看到的sql查询没有改变。对我来说不是一个问题,因为我可以使用beforSave和检查ID来知道我是插入还是更新,但我不知道为什么会发生这种情况。谢谢你在周日回答! :) – Jaume
beforeInsert在第二个参数中传递查询,这就是为什么您在模型中更改的值不再影响它。 'function beforeInsert($ q,$ m){ $ q-> set('somefield','someval'); },当然debug()也会有很大的帮助。 – romaninsh