creof / doctrine2-spatial

Doctrine2 multi-platform support for spatial types and functions.
MIT License
272 stars 174 forks source link

Cannot get geometry object from data you send to the GEOMETRY field #200

Open r8or0pz opened 5 years ago

r8or0pz commented 5 years ago

How do I store POINT type?

An exception occurred while executing 'INSERT INTO geo_point (address, coordinates) VALUES (?, ?)' with params ["Test", "POINT(-73.976683 40.760381)"]:

SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field

Result query:

INSERT INTO geo_point (address, coordinates) VALUES ('Test', 'POINT(-73.976683 40.760381)');

My Entity:

namespace BackendLib\Entity;

use Doctrine\ORM\Mapping as ORM;
use CrEOF\Spatial\PHP\Types\Geometry\Point;

class GeoPoint
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $address;

    /**
     * @ORM\Column(type="point", nullable=true)
     */
    private $point;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    public function setPoint(?Point $point): self
    {
        $this->point = $point;

        return $this;
    }

    public function getPoint(): ?Point
    {
        return $this->point;
    }
}

This is how I store it:

$obj = new GeoPoint();
$obj->setAddress('Test');
$obj->setPoint(new Point(40.7603807, -73.9766831));
$manager->persist($obj);
$manager->flush();
holtkamp commented 5 years ago

When looking at:

INSERT INTO geo_point (address, coordinates) VALUES ('Test', 'POINT(-73.976683 40.760381)');

It seems the POINT is handed over as a string, it should probably be:

INSERT INTO geo_point (address, coordinates) VALUES ('Test', POINT(-73.976683 40.760381));

Did you register the types and functions as done here:

https://github.com/creof/doctrine2-spatial/blob/58ea5fae1c1b450ee08d7dac25cd9e8f5e6fdebd/tests/CrEOF/Spatial/Tests/OrmTestCase.php#L191-L193

r8or0pz commented 5 years ago

I have found the reason. I use custom Quote Strategy class which adds quotes to table fields. Inspite it works with fields, somehow it affects values also. Here is the class: https://gist.github.com/lngphp/a6e9a5de1287604ad4ffb9d25c1abcb2

To be more specific, QuoteStrategy::getColumnName() is the reason.

holtkamp commented 4 years ago

So this issue can be closed then?