A
A
Acvoloe2018-05-04 05:37:14
Yii
Acvoloe, 2018-05-04 05:37:14

How to make double link yii2?

Hello!
How can you build a double bond in yii2?
Created a CRUD model through the Gii of the Client table. With the help of hasOne, a relationship with the Subnet table was inferred.
How can I display in the gridview of the Client model the relationship with Bras.name?
5aebc71f0a091497238533.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-05-04
@Acvoloe

client:

public function getSubnet(){
    return $this->hasOne(Subnet::class, ['id' => 'subnet']);
}
public function getBras(){
    return $this->hasOne(Bras::class, ['id' => 'bras'])->via('subnet');
    // или
    // return $this->hasOne(Bras::class, ['id' => 'bras'])->viaTable(Subnet::tableName(), ['id' => 'subnet']);
}

ClientSearch:
ClientSearch {
  public $brasName; // добавляем атрибут в модель поиска
}

grid view:
'id',
[
  'attribute' => 'brasName', // выводим в гриде
  'value' => function(Client $model){
    return $model->bras ? $model->bras->name : null;
  }
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question