yii2-relation-trait icon indicating copy to clipboard operation
yii2-relation-trait copied to clipboard

Can't save hasMany relation without index 0 (zero)

Open bpanatta opened this issue 7 years ago • 0 comments

As mentioned in the description of the extension, the hasMany relation array must have numerical keys:

            // has many
            [relationName] => Array 
                ( 
                    [0] => Array 
                        (
                            [relAttr] => relValue1
                        )
                    [1] => Array 
                        (
                            [relAttr] => relValue1
                        )
                )

And in the code we can see that the key 0 is required in deleteWithRelated and restoreWithRelated methods, that calls something like: $error = !$this->{$data['name']}[0]->updateAll($this->_rt_softdelete, ['and', $array]);

I have an relation with alphanumeric keys, which doesn't have a numerical index and gives a index not defined error. In order to solve it, I made a change to access the array data like the following:

array_values($this->{$data['name']})[0]

This approach is only valid to PHP 5.4+ though.

bpanatta avatar Mar 07 '18 15:03 bpanatta