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

Generic geometry object #107

Closed
SnakeO opened this issue Jan 29, 2024 · 2 comments
Closed

Generic geometry object #107

SnakeO opened this issue Jan 29, 2024 · 2 comments

Comments

@SnakeO
Copy link

SnakeO commented Jan 29, 2024

I have a column that can be either a Point or a Polygon, so this column has the generic geometry type. But I'm getting an error when I cast it.

Illuminate\Database\QueryException: SQLSTATE[HY093]: Invalid parameter number (SQL: insert into `areas` (`name`, `type`, `city_id`, `region_id`, `description`, `ai_description`, `country_id`, `geometry`, `centroid`, `updated_at`, `created_at`) values (Priceport, NH, sublocality_level_2, 1697, 3286, Tempore sint molestiae distinctio porro ab vel. Exercitationem quam illum rerum enim nam quidem. Ut soluta dicta quos iste., Et totam velit corrupti voluptatum nobis perferendis magnam. Quia non asperiores quas nihil. Qui sit at voluptate quam dolorem consequatur., 914, ST_GeomFromText('POINT(113.417617 -24.459282)', 4326, 'axis-order=long-lat'), ST_GeomFromText(2024-01-29 06:07:51, 2024-01-29 06:07:51, 'axis-order=long-lat'), ?, ?))

/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:720
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:546
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:498
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3322
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1869
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1330
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1295
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1138
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:330
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:235
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:339
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:283
/var/www/datacontent/tests/Feature/Controllers/Api/LocationControllerTest.php:54

Caused by
PDOException: SQLSTATE[HY093]: Invalid parameter number

/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:545
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:753
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:720
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:546
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:498
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3322
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1869
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1330
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1295
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1138
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:330
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:235
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:339
/var/www/datacontent/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:283
/var/www/datacontent/tests/Feature/Controllers/Api/LocationControllerTest.php:54
@MatanYadaev
Copy link
Owner

Hi @SnakeO, currently it's not supported. Making it work requires some changes and reactors, and I don't have time to invest in that at the moment.

I'll be happy to accept PRs and help.

@MatanYadaev MatanYadaev changed the title Is generic geometry supported? Generic geometry object Jan 30, 2024
@SnakeO
Copy link
Author

SnakeO commented Jan 30, 2024

This worked for me: I created a GeometryColumn class which I would cast my geometry column to:

<?php
/**
 * GeometryColumn should only be used as a cast in a 'geometry' column type.
 * It will cast the 'geometry' column to a specific Spatial object.
 */
class GeometryColumn implements Castable
{
    public static function castUsing(array $arguments): CastsAttributes
    {
        return new GeometryColumnCast(BaseGeometry::class);
    }
}

And the custom GeometryColumnCast simply bypasses the Exception that's thrown in the parent class:

/**
 * Handle casting the 'geometry' column to a specific Spatial object.
 */
class GeometryColumnCast extends BaseGeometryCast
{
    protected string $className;

    public function __construct(string $className)
    {
        parent::__construct($className);
        $this->className = $className;
    }

    public function set($model, string $key, $value, array $attributes): Expression|null
    {
        // If the value is an instance of the Spatial object, we return the SQL expression.
        if ($value instanceof $this->className) {
            return $value->toSqlExpression($model->getConnection());
        }

        return parent::set($model, $key, $value, $attributes);
    }
}

@SnakeO SnakeO closed this as completed Jan 30, 2024
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

No branches or pull requests

2 participants