我尝试按照文档中的说明来实现自定义类型“点”,但遇到了麻烦。我使用以下代码添加了文件:vendor/cakephp/cakephp/src/Database/Point.php:
<?php
namespace App\Database;
// Our value object is immutable.
class Point {
protected $_lat;
protected $_long;
// Factory method.
public static function parse($value) {
// Parse the data from MySQL.
return new static($value[0], $value[1]);
}
public function __construct($lat, $long) {
$this->_lat = $lat;
$this->_long = $long;
}
public function lat( {
return $this->_lat;
}
public function long() {
return $this->_long;
}
}
?>
然后我创建了文件:vendor/cakephp/cakephp/src/Database/Type/PointType.php,代码为:
<?php
namespace App\Database\Type;
use App\Database\Point;
use Cake\Database\Expression\FunctionExpression;
use Cake\Database\Type as BaseType;
use Cake\Database\Type\ExpressionTypeInterface;
class PointType extends BaseType implements ExpressionTypeInterface {
public function toPHP($value, Driver $d) {
return Point::parse($value);
}
public function marshal($value) {
if (is_string($value)) {
$value = explode(',', $value);
}
if (is_array($value)) {
return new Point($value[0], $value[1]);
}
return null;
}
public function toExpression($value) {
if ($value instanceof Point) {
return new FunctionExpression(
'POINT',
$value->lat(),
$value->long()
);
}
if (is_array($value)) {
return new FunctionExpression('POINT', $value[0], $value[1]);
}
// Handle other cases.
}
}
?>
然后我将这一行添加到 config/bootstrap.php 中:
Type::map('point', 'App\Database\Type\PointType');
将此行添加到 src/Model/Table/ForecastsTable.php:
use Cake\Database\Schema\Table as Schema;
protected function _initializeSchema(Schema $schema) {
$schema->columnType('location', 'point');
return $schema;
}
其中 location 是我的 MySQL 表 Forecasts 中的列名称,该表保存具有空间索引的 POINT 类型地理值。 如果我从任何页面访问该网站,我都会收到此致命错误两次:
致命错误:在中找不到类“App\Database\Type\PointType” C:\xampp\htdocs orecast_clash endo