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 App\DBAL\Types\Geolocation;
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 App\DBAL\Types\PSQL;
use App\DBAL\Types\Geolocation\Point;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
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: App\DBAL\Types\PSQL\PointType
In my location file I got the property coordinate, like this:
/**
* @ORM\Column(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.
SOLVED - The problem was on PointType class. I saw the raw data on database and it was in form "(value, value)". Only I need to change was the convertToPHPValue method.
Before:
list($longitude, $latitude) = sscanf($value, 'POINT(%f %f)');
After:
list($longitude, $latitude) = sscanf($value, '(%f, %f)');