Skip to content

Comments

Added fix to allow pivots to use classes on attach#120

Merged
LukeTowers merged 1 commit intodevelopfrom
wip/pivot-models-using-classes
Sep 27, 2022
Merged

Added fix to allow pivots to use classes on attach#120
LukeTowers merged 1 commit intodevelopfrom
wip/pivot-models-using-classes

Conversation

@jaxwilko
Copy link
Member

This PR ports some functionality from Laravel to allow for pivot models to be used during an attach call.

This means that events can now be processed on pivot models during the attach process.

@LukeTowers LukeTowers added this to the 1.2.1 milestone Sep 27, 2022
@LukeTowers LukeTowers merged commit 16f44dc into develop Sep 27, 2022
@LukeTowers LukeTowers deleted the wip/pivot-models-using-classes branch September 27, 2022 16:40
@mjauvin
Copy link
Member

mjauvin commented Sep 27, 2022

Doesn't that require a bit more unit tests before merging?

@LukeTowers
Copy link
Member

@mjauvin do we have one for the MorphToMany version of this? Merged it because @jaxwilko and I were both running into this issue and were able to confirm that this resolved the issue. Any concerns with it?

@mjauvin
Copy link
Member

mjauvin commented Sep 28, 2022

@LukeTowers @jaxwilko
My main concern is with the model.relation.beforeAttach event parameters being changed.

Any explanation for this?

@bennothommo
Copy link
Member

I agree - that's a bit of a behaviour change for the event there.

@LukeTowers
Copy link
Member

Hmm, I had thought that event was one of the recently added ones, although it looks like it has been around for a while, so I'll need to fix that.

@LukeTowers
Copy link
Member

@bennothommo @mjauvin is this better: e6e3c5b?

@mjauvin
Copy link
Member

mjauvin commented Sep 29, 2022

@LukeTowers much better!

@LukeTowers
Copy link
Member

Thanks for checking in on this guys :)

@ericp-mrel
Copy link
Contributor

I just wanted to report after pulling in the latest commit from winter/storm the following code no longer works for me:

$model->bindEvent('model.relation.afterAttach', function (string $relationName, array $attachedIDList, array $insertData) use ($model) {
    // ...
}

Error:

[2022-10-03 18:03:34] local.ERROR: ArgumentCountError: Too few arguments to function AbstractModelRelationHandler::{closure}(), 2 passed and exactly 3 expected in

The relation it's failing on is a morphToMany.

Reverting back to commit 5c46aa3 allows my code to work again.

@LukeTowers
Copy link
Member

@ericp-mrel can you toss a dd($eventArgs) on line 132 here: e6e3c5b#diff-2f413aba00b5b5f02604cf9c956287eb0656ab2ac79ef89ca9ee25ef6b324592R132 and report back the contents? The += might not be working correctly, might just have to switch to array_merge instead.

@ericp-mrel
Copy link
Contributor

array:2 [ // vendor/winter/storm/src/Database/Relations/Concerns/BelongsOrMorphsToMany.php:133
  0 => "videos"
  1 => array:1 [
    0 => array:4 [
      "video_id" => 9
      "videoable_id" => 1
      "videoable_type" => "ra_community"
      "order" => 1
    ]
  ]
]

Here's the results when using array_merge

Screen Shot 2022-10-04 at 9 01 16 AM

@ericp-mrel
Copy link
Contributor

I think you'd have to use array_merge because of how the + operator works with arrays it will only add to the original array if their keys are different and the right-hand array will not override the left-hand one. Using the + operator with arrays is usually only useful with associative arrays in my experience because of how it behaves.

@LukeTowers
Copy link
Member

Good point @ericp-mrel, I had forgotten that they weren't associative arrays. Since you reported the issue and tested the fix do you want to submit it as a PR to count towards Hacktoberfest for you?

@ericp-mrel
Copy link
Contributor

@LukeTowers I can submit a PR, but one thing I'm really curious about why is the event signature different for custom pivot model vs non-custom pivot model? I guess I'm not sure why they need to be different for event purposes and wish I knew the reason for changing it.

LukeTowers pushed a commit that referenced this pull request Oct 5, 2022
…ator. (#122)

This fixes the issue with the arrays not being merged properly with auto-incremented keys. See #120 (comment)
@LukeTowers
Copy link
Member

@ericp-mrel because the code that inserts the data with custom pivot models uses the unmodified values passed to the method and the code that inserts directly into the table uses the processed variables. Unfortunately when the events were originally added they were passed the processed variables instead of the raw ones, so we can't use just the raw variables for backwards compatibility reasons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants