Skip to content

[NFR]: Support POINT column type in MySQL #14769

Closed
Closed
@jesugmz

Description

@jesugmz

Describe the bug
Having a MySQL column with type POINT the reading of its value is not well retrieved.

To Reproduce

Phalcon Micro application:

<?php

use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Db\RawValue;
use Phalcon\Di\FactoryDefault;
use Phalcon\Http\Response;
use Phalcon\Mvc\Micro;
use Phalcon\Mvc\Model;

class Items extends Model
{
    /**
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=3, nullable=false)
     */
    public $id;

    /**
     * @var string
     * @Column(type="string", length=60, nullable=false)
     */
    public $location;

    public function initialize()
    {
        $this->setSchema('test');
        $this->setSource('items');
    }
}

$di = new FactoryDefault();
$di->setShared('db', function () {
    return new Mysql([
        'host'     => 'my-host',
        'username' => 'root',
        'password' => 'root',
        'dbname'   => 'test',
        'charset'  => 'utf8',
    ]);
});

$app = new Micro($di);

// broken
$app->get('/items/{id:[\d]}', function ($id) use ($app) {
  $item = Items::findFirst('id = '.$id);
  return $this->response->setJsonContent($item);
});

// works
$app->post('/items', function () use ($app) {
  $item = new Items();
  $item->location = new RawValue('ST_GeomFromText("POINT(1.0 1.0)")');
  $created = $item->save();

  return $created ? $this->response->setStatusCode(201) : $this->response->setStatusCode(500);
});

// works
$app->put('/items/{id:[\d]}', function ($id) use ($app) {
  $item = Items::findFirst('id = '.$id);
  $item->location = new RawValue('ST_GeomFromText("POINT(5.0 5.0)")');
  //$item->location = $item->location; // this will break as the the reading is broken
  $updated = $item->update();

  return $updated ? $this->response->setStatusCode(200) : $this->response->setStatusCode(500);
});

$app->notFound(function() {
    $response = new Response();
    return $response->setStatusCode(404);
});

$app->handle();

Database schema:

CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE test;

CREATE TABLE items (
  id MEDIUMINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  location POINT NOT NULL,
  PRIMARY KEY (id),
  SPATIAL INDEX (location)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

POST and PUT (writing a POINT column) operations will works but GET (reading a POINT column) will return a response like this:

{"id":"3","location":"\u0000\u0000\u0000\u0000\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0014@\u0000\u0000\u0000\u0000\u0000\u0000\u0014@"}

In Phalcon 3.4.4 the reading works well but not the writing which cannot modify the POINT column.

Details

  • Phalcon version: 4.0.2
  • PHP Version: 7.4.2
  • Operating System: Debian 10.2
  • Installation type: PECL
  • Server: Nginx
  • MySQL: 5.7.21-20

Metadata

Metadata

Assignees

Labels

new feature requestPlanned Feature or New Feature Request

Projects

Status

Released

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions