2

我正在尝试创建一个CitySeederusing Phinx。但我收到以下错误:

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

这是我的seeders/CitySeeder.php课。该geo_coords字段使用POINT数据类型:

<?php

use Phinx\Seed\AbstractSeed;

class CitySeeder extends AbstractSeed
{
    public function run()
    {
        $data = [
            [
                'name' => 'birmingham',
                'geo_coords' => 'POINT(0 0)'
            ],
            [
                'name' => 'london',
                'geo_coords' => 'POINT(0 0)'
            ],
            [
                'name' => 'liverpool',
                'geo_coords' => 'POINT(0 0)'
            ],
            [
                'name' => 'manchester',
                'geo_coords' => 'POINT(40 -100)'
            ],
        ];

        $cityTable = $this->table('city');
        $cityTable->insert($data)->save();
    }
}

这很奇怪,因为如果我手动将它输入到数据库中,它就可以工作。

经度/纬度是否必须以某种方式格式化?我试过使用数组和空格分隔的long lat格式,但我仍然得到同样的错误。我什至浏览了源代码,但找不到任何有用的东西。

任何帮助将非常感激。

编辑

我检查了发生错误的 Phinx 库中的代码:

public function insert(Table $table, $row)
{
    $this->startCommandTimer();
    $this->writeCommand('insert', array($table->getName()));

    $sql = sprintf(
        "INSERT INTO %s ",
        $this->quoteTableName($table->getName())
    );

    $columns = array_keys($row);
    $sql .= "(". implode(', ', array_map(array($this, 'quoteColumnName'), $columns)) . ")";
    $sql .= " VALUES (" . implode(', ', array_fill(0, count($columns), '?')) . ")";

    $stmt = $this->getConnection()->prepare($sql);

    $stmt->execute(array_values($row));
    $this->endCommandTimer();
}

array_values($sql)故障点的数据是:

array(2) {
    [0]=>
        string(10) "birmingham"
    [1]=>
        string(26) "POINT(0 0)"
}

并且设置了之后的查询$sql

string(55) "INSERT INTO `city` (`name`, `geo_coords`) VALUES (?, ?)"

prepare():之后执行die(var_dump($stmt->debugDumpParams()));以下操作时

SQL: [55] INSERT INTO `city` (`name`, `geo_coords`) VALUES (?, ?)
Params:  0
NULL

记录 MySQL 查询显示以下内容:

2016-12-12T12:53:12.721287Z    12 Query INSERT INTO `city` (`name`, `geo_coords`) VALUES ('birmingham', 'POINT(0, 0)')

我认为这是不正确的,因为 POINT 是作为字符串插入的?

4

1 回答 1

1

使用以下方法解决了问题:

<?php

use Phinx\Seed\AbstractSeed;

class CitySeeder extends AbstractSeed
{
    public function run()
    {
        $data = [
            [
                'name' => 'birmingham',
                'geo_coords' => [0, 0],
            ],
            [
                'name' => 'london',
                'geo_coords' => [0, 0],
            ],
            [
                'name' => 'liverpool',
                'geo_coords' => [0, 0],
            ],
            [
                'name' => 'manchester',
                'geo_coords' => [0, 0],
            ],
        ];

        $conn = $this->getAdapter()->getConnection();
        $sth = $conn->prepare('INSERT INTO city (`name`, `geo_coords`) VALUES (?, POINT(?, ?))');

        foreach($data as $key => $val)
        {
            $sth->execute([
                $val['name'],
                $val['geo_coords'][0],
                $val['geo_coords'][1]]
            );
        }
    }
}
于 2016-12-13T00:44:18.713 回答