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

Custom getConnectionName not called on extended MagicLink model due to usage of self instead of static #72

Closed
savander opened this issue Aug 10, 2022 · 1 comment · Fixed by #73

Comments

@savander
Copy link
Contributor

The latest change to the library broke my code:

$magiclink = new self();

I have extended your model class and done some modifications, such as overriding \Illuminate\Database\Eloquent\Model::getConnectionName to my custom one. The self keyword, do break the code, since it refers to the same class in which the new keyword is actually written. In that case, my getConnectionName is ignored and is not called.

The solution would be to use static since it uses late static binding, which refers to the class you called the method on.
It should not break your code, but enable it for further modification/extension.

Is there any reason to do that? Why would you restrict modification of your code?

In this case, other methods will also be affected, for example:

return self::where('id', $tokenId)
->where('token', $tokenSecret)
->where(function ($query) {
$query
->whereNull('available_at')
->orWhere('available_at', '>=', Carbon::now());
})
->where(function ($query) {
$query
->whereNull('max_visits')
->orWhereRaw('max_visits > num_visits');
})
->first();

@cesargb
Copy link
Owner

cesargb commented Aug 10, 2022

Please, feel free to submit a PR

savander pushed a commit to savander/laravel-magiclink that referenced this issue Aug 12, 2022
cesargb added a commit that referenced this issue Aug 13, 2022
* fixes issue #72 - use static instead of self in MagicLink class

* ingonre unsafe usage of new static

Co-authored-by: Adam Jaworski <[email protected]>
Co-authored-by: Cesar <[email protected]>
cesargb added a commit that referenced this issue Sep 25, 2022
…#74)

* fixes issue #72 - use static instead of self in MagicLink class

* ingonre unsafe usage of new static

Co-authored-by: Adam Jaworski <[email protected]>
Co-authored-by: Cesar <[email protected]>
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 a pull request may close this issue.

2 participants