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

Let SpatialExpression implement the toWkt() method #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vpratfr
Copy link

@vpratfr vpratfr commented Apr 6, 2020

This avoids that most spatial query functions fail when invoked from observer methods.

E.g.

Test setup

    $incomingMessage = new RawMessage();
    $incomingMessage->beacon_id = $boat->beacon_id;
    $incomingMessage->boat_id = $boat->id;
    $incomingMessage->timestamp = CarbonImmutable::now();
    $incomingMessage->type = MessageType::LOCATION_UPDATE();
    $incomingMessage->location_precision = 5;
    $incomingMessage->coordinates = new Point(5, 5);
    $incomingMessage->save();

Observer on RawMessage created() event

    $weatherAlerts = WeatherAlert::ongoing()
        ->atLocation($rawMessage->coordinates)
        ->get();

Crash

Error : Call to undefined method Grimzy\LaravelMysqlSpatial\Eloquent\SpatialExpression::toWkt()

Basically, in the observer, when doing ->atLocation($rawMessage->coordinates), the coordinates getter returns a SpatialExpression and not the original Point

This PR simply implements the toWkt method, in a harmless and perfectly safe way as it does the same thing as the existing getSpatialValue method

Fixes #115

@vpratfr
Copy link
Author

vpratfr commented Apr 6, 2020

I did not add any test as SpatialExpression does not have any of them and the code is very straightforward.

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.

Getting things to work in eloquent observer methods?
1 participant