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

Unknown database type point requested, Doctrine\DBAL\Platforms\MySQL80Platform may not support it. #2

Open
nkamuo opened this issue Mar 17, 2023 · 4 comments

Comments

@nkamuo
Copy link

nkamuo commented Mar 17, 2023

Please how do I resolve this Issue?

@BenMorel
Copy link
Member

BenMorel commented Mar 17, 2023

Hi, you need to register geo doctrine types:

use Doctrine\DBAL\Types\Type;
use Brick\Geo\Doctrine\Types\PointType;

Type::addType('Point', PointType::class);

If you're using Symfony:

doctrine:
  dbal:
    types:
      Point: Brick\Geo\Doctrine\Types\PointType
    mapping_types:
      Point: Point

@BenMorel BenMorel reopened this Mar 17, 2023
@BenMorel
Copy link
Member

Actually I'll leave this issue open until the README is updated with this info.

@nkamuo
Copy link
Author

nkamuo commented Mar 17, 2023 via email

@kor3k
Copy link

kor3k commented Aug 19, 2023

if you're using Symfony, you can do something like this:

<?php declare(strict_types=1);

namespace App\Geometry;

use Brick\Geo\Doctrine\Functions;
use Brick\Geo\Doctrine\Types;
use Brick\Geo\Engine;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Exception as DBALException;
use Symfony\Component\Finder\Finder;
use function Symfony\Component\String\u;

class GeometryDoctrineSetup
{
    private const TYPES_PATH = '/vendor/brick/geo-doctrine/src/Types';
    private const FUNCTIONS_PATH = '/vendor/brick/geo-doctrine/src/Functions';

    private const TYPES_NAMESPACE = 'Brick\\Geo\\Doctrine\\Types\\';
    private const FUNCTIONS_NAMESPACE = 'Brick\\Geo\\Doctrine\\Functions\\';

    private Engine\GeometryEngine $engine; 

    public function __construct(private readonly EntityManagerInterface $em, private readonly string $projectDir)
    {
    }

    public function initMappingTypes(): void
    {
        $finder = new Finder();
        $finder->in($this->projectDir.self::TYPES_PATH)->files()->name('*.php');

        foreach($finder as $file) {
            $name = u($file->getFilenameWithoutExtension())->trimSuffix('Type')->toString();
            $class = self::TYPES_NAMESPACE.$file->getFilenameWithoutExtension();

            try {
                Type::addType($name, $class);
            } catch (DBALException $e) {
                continue;
            }
            $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping($name, $name);
        }
    }

    public function initFunctions(): void
    {
        $finder = new Finder();
        $finder->in($this->projectDir.self::FUNCTIONS_PATH)->files()->name('*.php')->notName('Abstract*.php');

        foreach($finder as $file) {
            $name = sprintf('ST_%s', u($file->getFilenameWithoutExtension())->trimSuffix('Function')->toString());
            $class = self::FUNCTIONS_NAMESPACE.$file->getFilenameWithoutExtension();
            $this->em->getConfiguration()->addCustomStringFunction($name, $class);
        }
    }

    public function initEngine(): void
    {
        $this->engine = new Engine\PDOEngine(pdo: $this->em->getConnection()->getNativeConnection(), useProxy: true);
    }

    public function init(): void
    {
        $this->initMappingTypes();
        $this->initFunctions();
        $this->initEngine();
    }

    public function getEngine(): Engine\GeometryEngine
    {
        return $this->engine;
    }
}

and

services:
  App\Geometry\GeometryDoctrineSetup:
    lazy: false
    arguments:
      $em: '@doctrine.orm.default_entity_manager'
      $projectDir: '%kernel.project_dir%'
    tags:
      - { name: kernel.event_listener , event: kernel.request , method: init , priority: 999 }
      - { name: kernel.event_listener , event: console.command , method: init , priority: 999 }

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

3 participants