Converting DB field for display in activerecord field...

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?

-John

I would rather do it in "beforeSave", not in "beforeValidate". Changing the format before validation could lead to a validation error, I think.

Well I have changed beforeSave to read:




    public function beforeSave ( $insert )

    {

        echo 'before\n';

        // convert to storage format

        echo $this->days . '\n';

        $this->days = DateTime::packsessiondays( $this->days );

        echo $this->days . '\n';

        return parent::beforeSave($insert);

    }




And I can verify that the beforeSave() is working as I need.

However, the afterFind() is still not working…

I am getting the correct data into the function, and modifing

it correctly, but it does not seem to be getting returned…

I modified it to read:




    public function afterFind ()

    {

        // convert to display format

        echo 'before\n';

        echo $this->days . '\n';

        $this->days = 'testing';  // <-------- for testing only....

        echo $this->days . '\n';

        parent::afterFind();

        return true;

    }



And $this->days is still coming up blank.

It seems like it is not being returned…

But it should be local to the model class… Shouldn’t it???

-John

Would you please try checking the value after "parent::afterFind()" ?




    public function afterFind ()

    {

        // convert to display format

        echo 'before\n';

        echo $this->days . '\n';

        $this->days = 'testing';  // <-------- for testing only....

        echo $this->days . '\n';

        parent::afterFind();

        echo $this->days . '\n';  /* check it */

        return true;

    }



OK, it looks like I updated one of the associated files on my system,

but did not get it uploaded to the server.

It probably was taking the correctly converted string from afterFind()

and trying to convert it again. Needless to day, that second conversion

did not go well…

Programmer error!!!!

Thanks for looking this over and helping.

-John