Return Point type PSQL and DOCTRINE

  orm, php, postgis, psql, symfony

I got an app that use POSTGIS Point value. I store it in my location table as coordinate. I can setup a new Point to database using Symfony and doctrine. The problem is the inverse, when I try to get te point on symfony I cant, the show me a blank value or null. I got the next configuration files:

Point.php:

<?php

namespace AppDBALTypesGeolocation;

class Point
{
    private $latitude;
    private $longitude;

    /**
     * Point constructor.
     */
    public function __construct($latitude, $longitude)
    {
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }

    /**
     * @return mixed
     */
    public function getLongitude()
    {
        return $this->longitude;
    }

    /**
     * @return mixed
     */
    public function getLatitude()
    {
        return $this->latitude;
    }
}

PointType.php

<?php

namespace AppDBALTypesPSQL;

use AppDBALTypesGeolocationPoint;
use DoctrineDBALPlatformsAbstractPlatform;
use DoctrineDBALTypesType;

class PointType extends Type
{

    const POINT = 'point';

    /**
     * @param array $column
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getSQLDeclaration(array $column, AbstractPlatform $platform)
    {
        return "POINT";
    }

    /**
     * @return string
     */
    public function getName()
    {
        return self::POINT;
    }

    /**
     * @param mixed $value
     * @param AbstractPlatform $platform
     * @return Point
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        list($longitude, $latitude) = sscanf($value, 'POINT(%f %f)');
        return new Point($latitude, $longitude);
    }

    /**
     * @param mixed $value
     * @param AbstractPlatform $platform
     * @return string
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return $value->getLongitude() . ',' . $value->getLatitude();
    }

    /**
     * @param string $sqlExpr
     * @param AbstractPlatform $platform
     * @return string
     */
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
    {
        return sprintf('PointFromText(%s)', $sqlExpr);
    }
}

doctrine.yaml:

doctrine:
    types:
        point: AppDBALTypesPSQLPointType

In my location file I got the property coordinate, like this:

 /**
 * @ORMColumn(type="point")
 */
private $coordinate;

public function getCoordinate(): Point
{
    return $this->coordinate;
}

public function setCoordinate($coordinate): self
{
    $this->coordinate = $coordinate;

    return $this;
}

After I call the getCoordinate function and then getLatitude they return me null value. I dont know how to work with It, I try various methods I find on stackoverflow, but anyone works to me.

Source: Ask PHP

LEAVE A COMMENT