Model getter/setter not working for me?

New to Yii. Unsure why this does not work.


<?php


namespace app\models\video;


use Yii;


/**

 * This is the model class for table "vjobq4".

 *

 * @property integer $id

 * @property integer $vid

...

 * @property string $encoded

 * @property string $meta

 * @property integer $priority

 */

class Job extends \yii\db\ActiveRecord

{

...


    /**

     * @inheritdoc

     */

    public function rules()

    {

        \stewlog::bm('model/video/Job  JOB RULES');

        return [

...		

            [['status', 'data', 'result', 'errors', 'encoded','meta'], 'string'],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        \stewlog::bm('model/video/Job  ATTR LABS');


        return [

            'id' => Yii::t('app', 'ID'),

            'vid' => Yii::t('app', 'Vid'),

...

            'meta' => Yii::t('app', 'Meta'),

            'priority' => Yii::t('app', 'Priority'),

        ];

    }


    /**

     * @inheritdoc

     * @return JobQuery the active query used by this AR class.

     */

    public static function find()

    {

        \stewlog::bm('model/video/Job  FIND');

        return new JobQuery(get_called_class());

    }


    public function setMeta($v)

    {

        \stewlog::bm('model/video/Job  SETMETA');


        return json_decode(serialize($v));

    }

    public function getMeta()

    {

        \stewlog::bm('model/video/Job  GETMETA');

        

        return json_encode(unserialize($this->meta));

    }


}



I can verify this model is the one used, but I never see the call to getMeta.

I expected to see the "meta" field unserialized and formatted as JSON. I see the raw mysql data instead, and I get no notice in my log that GETMETA was called ?

What have I overlooked?

Isn’t “meta” a column in the table? Then getMeta() and setMeta() won’t be called when you access “meta”.

http://www.yiiframework.com/doc-2.0/guide-concept-properties.html

And, probably what you want to do can be implemented by overriding "afterFind" and "beforeSave".

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#afterFind()-detail

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#beforeSave()-detail

Thank you very much for pointing that out.

Too many years with other tools, my head was thinking Varien Object. Or some darn thing.

Ok so here is a sample of what worked. This translates dates stored as INT(11) into human readable strings.


    public function init()

    {

        $this->on(self::EVENT_AFTER_FIND, [$this, 'onAfterFind']);

    }

...

    public function onAfterFind($ev)

    {

        $this->tm_encode_start = date('Y-m-d', $this->tm_encode_start);

        $this->tm_encode_end = date('Y-m-d', $this->tm_encode_end);

        $this->tm_upload = date('Y-m-d', $this->tm_upload);

    }



In my terms, getters and setters on the ActiveRecord are for "calculated fields" only. They may not exist in the table.

To mungle actual data we need events. Or, I could invent me own getter, so long as I named it uniquely w.r.t. existing column names.

Experience says that will not cover all the bases. I hope events are at a sufficiently low level so that the bases are buried.

Again thank you. I was head-banging and refused to read that sentence!