Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for morphTo relations broken in 4.1 #2657

Closed
apeisa opened this issue Oct 31, 2023 · 4 comments · Fixed by #2669
Closed

Support for morphTo relations broken in 4.1 #2657

apeisa opened this issue Oct 31, 2023 · 4 comments · Fixed by #2669
Labels
relations Relations between documents

Comments

@apeisa
Copy link
Contributor

apeisa commented Oct 31, 2023

  • Laravel-mongodb Version: 4.1.dev
  • PHP Version: 8.2.11
  • Database Driver & Version: 1.16.2 (6.0.3)

Description:

There has been working support for morphTo relations, but that seems to break after upgrading from 4.0 to 4.1.

Steps to reproduce

  1. You need to have model with morphTo relationship. Here is my "Subscription" model (it does extend mongo model, leaving namespaces out for simpllicity):
class Subscription extends Model
{
    use HasFactory;

    public function subscribable(): MorphTo
    {
        return $this->morphTo();
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
  1. When using the relationship it fails. This is how this is used in our codebase (controller):
$subscription = new Subscription();
$subscription->user()->associate($user);
$subscription->subscribable()->associate($thread);
$subscription->save();

Expected behaviour

New subscription should be saved to database with this kind of values:

{
  "_id": {
    "$oid": "647ee3c2ef95cab3850d4ee9"
  },
  "user_id": "64637d18a21fdb655d09bc83",
  "subscribable_id": "647ee3c2ef95cab3850d4ee8",
  "subscribable_type": "App\\Models\\Thread",
  "updated_at": {
    "$date": "2023-06-06T07:44:02.589Z"
  },
  "created_at": {
    "$date": "2023-06-06T07:44:02.589Z"
  }
}

Actual behaviour

InvalidArgumentException is thrown: First argument of MongoDB\Laravel\Query\Builder::where must be a field path as "string". Got "null"

Here is more detailed stacktrace: https://flareapp.io/share/bP99dGOP

@GromNaN GromNaN added the relations Relations between documents label Nov 1, 2023
@apeisa
Copy link
Contributor Author

apeisa commented Nov 6, 2023

I also noticed that MorphTo relations are not listed as supported relations on the readme. They have worked nicely before though - so is it safe to assume these will be supported in the future too? Because if not, this would mean migrating away from Mongo in our current project.

@hans-thomas
Copy link
Contributor

Hi @apeisa, I was working on this issue today. I fixed it and I'm going to open a PR.

@GromNaN
Copy link
Member

GromNaN commented Nov 6, 2023

Thanks for your feedback @apeisa, it's great to have contributors testing development branches. It helps us deliver stable releases without regression.
We're going to make sure there's no regression with MorphTo. This feature will be covered by tests from now on.

@apeisa
Copy link
Contributor Author

apeisa commented Nov 6, 2023

Great news and thanks for pull request. I will do further tests when it is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relations Relations between documents
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants