I have this schema:
and this relation in model zwz:
public function getAuftrs() {
return $this->hasMany(\app\models\Auftr::className(), ['id' => 'auftr_id'])
->viaTable('znw', ['zwzx_id' => 'id'])
->viaTable('zwz_expl', ['zwz_id' => 'id'])
;}
in the view of zwz:
<?= count($model->getAuftrs()->asArray()->all())
I'm getting:
PHP Notice – yii\base\ErrorException
Undefined index: auftr_id
- in C:...\vendor\yiisoft\yii2\db\ActiveRelationTrait.php
And now if I change the two viaTable()s to:
->via('znws')
and of course define this relation before:
public function getZnws() {
return $this->hasMany(\app\models\Znw::className(), ['zwzx_id' => 'id'])
->viaTable('zwz_expl', ['zwz_id' => 'id'])
;}
then it works.
The problem is, that this latter via() way is incompatible with yii2-giiant, so I would like to know what is the difference actually between the two, and how could I keep the original viaTable() way.
for me it seems quite clear that we always have to pick the last ID of the chain and define all other IDs backwards. (however in these docs there is via() and not viaTable() and maybe it makes also a difference)
Thanks in advance!

You can not use
viaTable()twice on the same relation. The second call will overwrite the first one. If you want to go over more than a junction table you need via(). You can however define multiple relations, one of them usingvia()and the other usingviaTable().I have no idea how giiant works, but it may detect a Many-Many relation through the fact that
viaTable()is used.viaTable()in contrast tovia()skips one table so you do not need an ActiveRecord for the junction table. Withvia()you always define direct relations.About the order of keys in relation definitions, please check the docs at