Coder Social home page Coder Social logo

geo-doctrine's Introduction

Brick

Incubator for PHP components under development.

Build Status Coverage Status License

Once a component reaches sufficient maturity, it will be moved to its own repository and composer package, and enter a beta phase. For this reason, this repository will never contain releases.

The incubator can be included in your project using Composer. Just define the following requirement in your composer.json file:

{
    "require": {
        "brick/brick": "dev-master"
    }
}

Feel free to use the incubator for your own use/research, but please note that the API can, and will, change at any time. Components can also disappear at any time.

geo-doctrine's People

Contributors

benmorel avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

docentbf

geo-doctrine's Issues

Help using on symfony controller

I'm trying to use PointType in my symfony project, but I don't know how to use on my controller. Can you help me or give an example please?

Here my config/packages/doctrine.yaml config:

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

Then in my Entity:

use Brick\Geo\Doctrine\Types\PointType as Point;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User 
{
   
    #[ORM\Column(type: 'Point', nullable:true)]
    private $latlong;

    public function getLatlong(): ?Point
    {
        return $this->latlong;
    }

    public function setLatlong(Point $latlong): self
    {
        $this->latlong = $latlong;

        return $this;
    }

And, how can I use $user->setLatlong(); inside of my controller?

    public function myfunction(Request $request, EntityManagerInterface $entityManager): Response
    {   
        $user = new user();
        $pointType = //how can create PointType 
        $user->setLatlong($pointType);
        $entityManager->persist($user);
        $entityManager->flush();

        return $this->render('mytemplate.html.twig');
    }

Thanks

The annotation "@noproxy" in method Brick\Geo\Geometry::SRID() was never imported. Did you maybe forget to add a "use" statement for this annotation?

I was unable to use the library at all since I was unable to get the mapping to work due to this exception:
The annotation "@noproxy" in method Brick\Geo\Geometry::SRID() was never imported. Did you maybe forget to add a "use" statement for this annotation?

In fact I was lost due to the lack of documentation and ended up abandoning this promising package.

Is there someone who managed to get this package to work?

DoctrineEngine

@BenMorel hello, can you please add DoctrineEngine to the lib?
i am using this one, based on PDOEngine. it works, but perhaps needs your check if it it's all correct.

one big advantage of using doctrine over pdo is that doctrine queries get logged.
so you can inspect all the actual queries made, e.g. in symfony profiler.

<?php declare(strict_types=1);

namespace App\Geo\Doctrine;

use Brick\Geo\Engine\DatabaseEngine;
use Brick\Geo\Engine\GeometryParameter;
use Brick\Geo\Exception\GeometryEngineException;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Exception as DBALException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Statement;
use Doctrine\ORM\EntityManagerInterface;

class DoctrineEngine extends DatabaseEngine
{
    /** @var Statement[] */
    private array $statements = [];

    public function __construct(
        private readonly EntityManagerInterface $em,
        bool $useProxy = true,
    ) {
        parent::__construct($useProxy);
    }

    protected function getDBAL(): Connection
    {
        return $this->em->getConnection();
    }

    protected function executeQuery(string $query, array $parameters): array
    {
        try {
            if (!isset($this->statements[$query])) {
                $this->statements[$query] = $this->getDBAL()->prepare($query);
            }

            $statement = $this->statements[$query];

            $index = 1;

            foreach ($parameters as $parameter) {
                if ($parameter instanceof GeometryParameter) {
                    $statement->bindValue($index++, $parameter->data, $parameter->isBinary ? ParameterType::LARGE_OBJECT : ParameterType::STRING);
                    $statement->bindValue($index++, $parameter->srid, ParameterType::INTEGER);
                } else {
                    if (is_int($parameter)) {
                        $type = ParameterType::INTEGER;
                    } else {
                        $type = ParameterType::STRING;
                    }

                    $statement->bindValue($index++, $parameter, $type);
                }
            }

            $result = $statement->getWrappedStatement()->execute();
            $result = $result->fetchNumeric();
        } catch (DBALException $e) {
            $errorClass = substr((string) $e->getSQLState(), 0, 2);

            // 42XXX = syntax error or access rule violation; reported on undefined function.
            // 22XXX = data exception; reported by MySQL 5.7 on unsupported geometry.
            if ('42' === $errorClass || '22' === $errorClass) {
                throw GeometryEngineException::operationNotSupportedByEngine($e);
            }

            throw $e;
        } catch (\Throwable $e) {
            $errorClass = substr((string) $e->getCode(), 0, 2);

            // 42XXX = syntax error or access rule violation; reported on undefined function.
            // 22XXX = data exception; reported by MySQL 5.7 on unsupported geometry.
            if ('42' === $errorClass || '22' === $errorClass) {
                throw GeometryEngineException::operationNotSupportedByEngine($e);
            }

            throw $e;
        }

        assert(false !== $result);

        return $result;
    }

    protected function getGeomFromWKBSyntax(): string
    {
        if ($this->isMySQL()) {
            return 'ST_GeomFromWKB(BINARY ?, ?)';
        }

        return parent::getGeomFromWKBSyntax();
    }

    protected function getParameterPlaceholder(string|float|int $parameter): string
    {
        if ($this->isPostgreSQL()) {
            if (is_int($parameter)) {
                // https://stackoverflow.com/q/66625661/759866
                // https://externals.io/message/113521
                return 'CAST (? AS INTEGER)';
            }
        }

        return parent::getParameterPlaceholder($parameter);
    }

    protected function isMySQL(): bool
    {
        return $this->getDBAL()->getDatabasePlatform() instanceof MySQLPlatform;
    }

    protected function isPostgreSQL(): bool
    {
        return $this->getDBAL()->getDatabasePlatform() instanceof PostgreSQLPlatform;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.