Setter related to an attribute model, doesn't dispatch

Hello.

In my model file (Articles.php) I had this:


    protected $date_end; //'date_end' Is a model attribute, do not a table field

    protected function getdate_end()

    {

       Yii::trace("Enter Get","file");        

    }


    protected function setdate_end()

    {

        Yii::trace("Enter Set","file");        

    }



Well… why is the Getter dispatched, and the Setter doesn’t?

Usually the attribute for such kind of property has a different name, try renaming _date_end, for example.

also di the signature of the setter methods expect a paramter in input (the value to set).

Changed to:


    protected $_date_end; //'date_end' Is a model attribute, do not a table field

    protected function get_date_end()

    {

       Yii::trace("Enter Get","file");        

    }


    protected function set_date_end($par)

    {

        Yii::trace("Enter Set","file");        

    }

… and that method is still the setter not working ???

What’s wrong?

The correct way to use is:




$private $_date_end // or protected...


public function getDate_end(){

   return $this->_date_end;

}


public function setDate_end($val){

   $this->_date_end = $val;

}


//Usage...


$d = $model->date_end;

$model->date_end = '2010-10-06';



Obviusly, because getXXX and setXXX are function, you can do your own stuff…

Wow PoL, thanks. I expected the setter was dispatched when model was saved, but I have realised I was wrong about that. Due to the getter method was called in edition mode, i expected the setter method was called when saving.

Is The correct method to manage data before saving to use beforeSave() overriding? If yes, Is this correct?


 protected function beforeSave()

    {

        

        if(parent::beforeSave())

        {

             //manage fields     

                

            return true;

        }

        else

            return false;

    }

Is that correct, or you propose something better?

I think is good…