【问题标题】:Changing values in BeforeInsert or BeforeSave hooks在 BeforeInsert 或 BeforeSave 挂钩中更改值
【发布时间】:2012-06-17 06:51:08
【问题描述】:

我正在用一个非常简单的课程来做这件事,只是为了学习,但我无法让它像我在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();
    }  
}

我也尝试了 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();
        });  
    }
}

在模型描述的两个实现中,都使用插入表单中的值保存,而不是使用“测试”。如果我在 beforeTest 函数中回显 $this->description 或 $m->description 在我设置它之前它是空的,然后是“测试”,但它不会对生成的 sql 产生任何影响。当然我错过了一些东西,但是¿什么?

谢谢!!

【问题讨论】:

    标签: hook atk4


    【解决方案1】:

    改变值的正确方法是:

    $this['description'] = 'test;
    

    【讨论】:

    • 感谢罗曼!我以这种方式开始并更改为 -> 以查看它是否有效,然后我使用 thas 语法发送了我的示例,但我之前检查了 $this['description']。
    • 感谢罗曼!我以这种方式开始并更改为 -> 以查看它是否有效,并且我使用 thas 语法发送了我的示例,但我之前检查了 $this['description'] 。现在我再次检查它,我发现它在 beforeSave 中有效,但在 BeforeInsert 中无效。在 beforeInsert 中启动了钩子,但我在调试模式下看到的 sql 查询没有改变。对我来说不是问题,因为我可以使用 beforSave 并检查 id 以了解我是在插入还是更新,但我不知道为什么会发生这种情况。谢谢周日回复!! :)
    • beforeInsert 在第二个参数中传递了一个查询,这就是您在模型中更改的值不再影响它的原因。 function beforeInsert($q,$m){ $q->set('somefield','someval'); },当然 debug() 也会有很大帮助。
    • 我在此处增强了文档:agiletoolkit.org/learn/understand/model/actions 以详细解释每个挂钩的工作原理以及何时使用。
    猜你喜欢
    • 2022-10-25
    • 2018-06-29
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多