0

I'm doing this with a very simple class, just to learn, but I'm not able to have it working like I see in http://agiletoolkit.org/learn/understand/model/actions

This is the class definition:

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();
    }  
}

I tried whih the samble page format too:

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;
    }
}

The task test page is simple too:

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();
        });  
    }
}

On both implementqtions of model description is saved with the value inserted in the form, not with 'Test'. If I echo $this->description or $m->description inside beforeTest function it's empty before I set it and 'Test' after, but it dos not make nothing whith the generated sql. Sure I'missing somethig, but ¿WHAT?

Thanks!!

Jaume
  • 63
  • 4

1 Answers1

0

The correct way to change value is:

$this['description'] = 'test;
romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Thanks Roman! I started that way and changet to -> to see if it works, and I sent my sample with thas syntax, but I checked $this['description'] before. – Jaume Jun 17 '12 at 14:24
  • Thanks Roman! I started that way and changet to -> to see if it works, and I sent my sample with thas syntax, but I checked $this['description'] before. Now I have checked it again and I see it works in beforeSave, but not in BeforeInsert. In beforeInsert the hook is launched but the sql query I see in debug mode is not changed. Not a problem for me because I can use beforSave and check id to know if I'm inserting or updating, but I don't know why this happens. Thanks for answering in sunday!! :) – Jaume Jun 17 '12 at 14:31
  • beforeInsert is passed a query in 2nd argument, that's why the values you change in the model do not longer affect it. `function beforeInsert($q,$m){ $q->set('somefield','someval'); }`, of course debug() would greatly help too. – romaninsh Jun 19 '12 at 04:36
  • I have enhanced the documentation here: http://agiletoolkit.org/learn/understand/model/actions to explain how each hooks works in details and when it should be used. – romaninsh Jun 19 '12 at 05:06