Relational data in Data widgets DetailView

Hello, please I need help, 'cause don’t know where I’m wrong.

In an yii2 app I have two tables:


CREATE TABLE IF NOT EXISTS `umarker` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID Segnalazione',

  `status_id` int(10) unsigned NOT NULL COMMENT 'Stato segnalazione',

  PRIMARY KEY (`id`),

  KEY `status_id` (`status_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;


ADD CONSTRAINT `umarker_ibfk_2` FOREIGN KEY (`status_id`) REFERENCES `umarkerstat` (`id`),


CREATE TABLE IF NOT EXISTS `umarkerstat` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `status` varchar(60) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;



In Umarker.php there is relational data field


    public function getStatus()

    {

        return $this->hasOne(Umarkerstat::className(), ['id' => 'status_id']);

    }



Then, in view there are


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

             'id',

	     'status_id',

        ],

    ]) ?>

Of course I will prefer to show text field instead of id. So I tried:


        'attributes' => [

            'id',

	    'umarkerstat.status_id',

        ],

and


        'attributes' => [

            'id',

			 [

    	        'label' => 'Status',

    	        'value' => $model->umarkerstat->status_id,

	        ],

        ],

In both cases, obtaining:


Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\Umarker::umarkerstat

where am I wrong, please?

You named your getter “getStatus()” so your property/relation name is “status”. You can access it using $model->status->status_id or ‘status.status_id’

Thank you Dawid, your answer sounds good, but coming to the same error.


Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\Umarkerstat::status_id


    1. in /var/www/mds/vendor/yiisoft/yii2/base/Component.php at line 143

    134135136137138139140141142143144145146147148149150151152


                foreach ($this->_behaviors as $behavior) {

                    if ($behavior->canGetProperty($name)) {

                        return $behavior->$name;

                    }

                }

            }

            if (method_exists($this, 'set' . $name)) {

                throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);

            } else {

                throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);

            }

        }

     

        /**

         * Sets the value of a component property.

         * This method will check in the following order and act accordingly:

         *

         *  - a property defined by a setter: set the property value

         *  - an event in the format of "on xyz": attach the handler to the event "xyz"


    2. in /var/www/mds/vendor/yiisoft/yii2/db/BaseActiveRecord.php – yii\base\Component::__get('status_id') at line 246

    3. in /var/www/mds/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php – yii\db\BaseActiveRecord::__get('status_id') at line 190

    4. in /var/www/mds/vendor/yiisoft/yii2/widgets/DetailView.php – yii\helpers\BaseArrayHelper::getValue(app\models\Umarker, 'status.status_id') 

Ok, I got it. The filed name in relate model wasn’t “status_id”, but “status”, so:

‘status.status’

works fine.

Thank you Dawid, to drive me in the right direction.