Skip to content

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

@bpanatta

Description

@bpanatta

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions