I have a encoded database field that needs to be decoded for display / update in a active record field in a view.
If a record is created or updated it needs to be encoded before it is saved back to the database.
I am tried adding the following functions to my model to handle it for me, but it does not work…
// as soon as we get a record, convert the encoded days field for display / edit
public function afterFind ()
{
// convert to display format
echo 'before\n';
echo $this->days . '\n';
$this->days = DateTime::unpacksessiondays( $this->days );
echo $this->days . '\n';
parent::afterFind ();
}
// before we save the text days field, encode it for the db
public function beforeValidate ()
{
echo 'before\n';
// convert to storage format
echo $this->days . '\n';
$this->days = DateTime::packsessiondays( $this->days );
echo $this->days . '\n';
return parent::beforeValidate ();
}
I added the echo statements so I could see that my functions are working correctly, and they are.
I think I got this idea from a yii 1.1 post, but it does not look like anything has changed for yii 2.x.
Can anyone give me some insight into what is worng here?