Skip to content

fix for returning incorrect builder in hybrid relations #1326

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

Merged
merged 2 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ protected function guessBelongsToManyRelation()
*/
public function newEloquentBuilder($query)
{
return new EloquentBuilder($query);
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return new Builder($query);
} else {
return new EloquentBuilder($query);
}
}
}
3 changes: 2 additions & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed right? Since Eloquent had this trait as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean, but I added the HybridRelations trait for the User model as using that trait immediately gives an error without this fix. In the first commit I used 2 use statements which I replaced to be 1 statement, therefor this line shows as deleted.

I dont think the Eloquent model class (either mongodb's or laravel's) implements one of these traits already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jenssegers\Mongodb\Eloquent\Model already has the HybridRelations trait, which the model extends from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. Thanks for merging.

use Authenticatable, CanResetPassword, HybridRelations;

protected $connection = 'mongodb';
protected $dates = ['birthday', 'entry.date'];
Expand Down