octobercms / test-plugin

Test Suite & Playground for October CMS
134 stars 95 forks source link

issue with relation attach event payload #113

Closed chrisvidal closed 7 months ago

chrisvidal commented 8 months ago

in the model user.php , the __construct is listening for new relation to be attached or detached.

the issue is that the parameters attributes is always null and does not contains the pivot data as expected.

$this->bindEvent('model.relation.attach', function ($relationName, $ids, $attributes) {
            traceLog("New relation {$relationName} was created", $ids, $attributes);
        });

the log outputs:

[2023-12-29 17:06:04] local.INFO: New relation roles_pivot was created  
[2023-12-29 17:06:04] local.INFO: Array
(
    [0] => 2
)

[2023-12-29 17:06:04] local.INFO: Array
(
)
EdvinasBaranauskas commented 7 months ago

Facing the same issue and i cant find any fix or a work around. did you manage to get something to work?

chrisvidal commented 7 months ago

nope I didn't for this one...still no ids and no attributes vcalues

EdvinasBaranauskas commented 7 months ago

few hours ago i figured a way with PivotModel. On relationship definition in the Model, added extra prop PivotModel => MyClass::class. the MyClass extends Pivot from use October\Rain\Database\Pivot;.

and inside of that custom class, in the boot function i added

static::saving(function ($pivot) {
   // logic ...
}

and in here $pivot has all the data. Its a shame you have to create a whole new class literally just for this but It works. atleast in my case.

daftspunk commented 7 months ago

This should be fixed in v3.6 which will be released soon.