I did this for the grades module in which I have parent-child relationships for classes.
Follow these steps:
In this model file, write this code for self-attachment. We need a table name alias. Here "parent" is an alias.
public function getParent() { return $this->hasOne(self::classname(), ['id' => 'parent_id'])-> from(self::tableName() . ' AS parent'); }
Write this code in your view file
[ 'label' => 'Parent Grade', 'value' => ($model->parent_id != '' || $model->parent_id != 0) ? $model->parent->name : 'Root Grade', ],
Here, in my code, if the parent_id field parent_id not null / zero, it displays the parent name of the class otherwise "Root Grade".
source share