schmunk42 / yii2-giiant

Yii 2 Framework Code Generator Gii on Steroids
271 stars 120 forks source link

Generating CRUD with via() and many-to-many relations gives an exception. #298

Open KevinVanH opened 11 months ago

KevinVanH commented 11 months ago

In the following line https://github.com/schmunk42/yii2-giiant/blob/0b063fd4ec31e29cee62ba17f97a370d788f787b/src/generators/crud/default/views/view.php#L161

it tries to access $relation->via->from[0]. However, this does not exist. Via is an array with keys 0, 1, 2 as shown below. The following error is thrown when generating the CRUD:

PHP Warning 'yii\base\ErrorException' with message 'Attempt to read property "from" on array'

$relation->via looks like this: image

It seems like this happens only with many-to-many relations when using via(). (JUNCTION_RELATION_VIA_MODEL)

Changing $relation->via->from[0] to $relation->via->from[0] ?? $relation->via[0] seems to fix it, but I'm not sure if this is correct.

Giiant 0.15.0-beta2 Yii2 2.0.49.2