I tried to follow the instructions in the documentation to implement a custom type ‘point,’ but I am having trouble. I added file: vendor/cakephp/cakephp/src/Database/Point.php with this code:
<?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; } } ?>
Then I created file: vendor/cakephp/cakephp/src/Database/Type/PointType.php with code:
<?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.
}
}
?>
Then I added this line to config/bootstrap.php:
Type::map(‘point’, ‘App\Database\Type\PointType’);
and this line to src/Model/Table/ForecastsTable.php:
use Cake\Database\Schema\Table as Schema;
protected function _initializeSchema(Schema $schema) {
$schema->columnType(‘location’, ‘point’);
return $schema;
}
where location is the name of the column in my MySQL table Forecasts which holds POINT type geo values with a spatial index.
If I access the website from any page, I get this Fatal Error twice:
Fatal error: Class ‘App\Database\Type\PointType’ not found in C:\xampp\htdocs\forecast_clash\vendor\cakephp\cakephp\src\Database\Type.php on line 106
Here is the function called in Type.php with line 106 *'d
public static function build($name) {
if (isset(static::$_builtTypes[$name])) {
return static::$_builtTypes[$name];
}
if (!isset(static::$_types[$name])) {
throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name));
}
if (is_string(static::$_types[$name])) {
**return static::$_builtTypes[$name] = new static::$_types[$name]($name);**
}
return static::$_builtTypes[$name] = static::$_types[$name];
}
I am new to CakePHP and am really struggling to decipher the errors and figure out where I went wrong. Any help is appreciated!