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!