Last active
July 24, 2023 01:43
-
-
Save Lawrence72/0fada42e673abd41600190b4035dfad3 to your computer and use it in GitHub Desktop.
Test for Geo Spacial in Fat Free Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
DROP TABLE IF EXISTS `locations`; | |
CREATE TABLE `locations` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`name` varchar(100) DEFAULT NULL, | |
`position` point NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; | |
INSERT INTO `locations` (`id`, `name`, `position`) VALUES | |
(1, 'point_1', ST_GeomFromText('POINT(1 1)', SRID(position))), | |
(2, 'point_2', ST_GeomFromText('POINT(2 2)', SRID(position))), | |
(3, 'point_3', ST_GeomFromText('POINT(3 3)', SRID(position))); | |
*/ | |
declare(strict_types=1); | |
namespace app\Controller; | |
final class Index_Controller { | |
public function indexAction($f3, array $args = []) { | |
} | |
public function getsqlAction($f3, array $args = []) { | |
$DB = $f3->get('DB'); | |
$locations = $DB->exec('SELECT `name`,ST_ASTEXT(`position`) as position FROM locations'); | |
echo json_encode($locations); | |
} | |
public function setsqlAction($f3, array $args = []) { | |
$DB = $f3->get('DB'); | |
$locations = $DB->exec('INSERT INTO locations (`name`,`position`) VALUES ("test",ST_GeomFromText("POINT(4 4)"))'); | |
echo json_encode($locations); | |
} | |
public function getMapperAction($f3, array $args = []) { | |
$Location_Mapper = new \app\Mapper\Location_Mapper($f3->get('DB')); | |
$Location_Mapper->point = 'ST_ASTEXT(position)'; | |
$locations = $Location_Mapper->find(); | |
$location_data = []; | |
foreach ($locations as $location) { | |
$location_data[] = [ | |
'name' => $location->name, | |
'point' => $location->point | |
]; | |
} | |
echo json_encode($location_data); | |
} | |
public function setMapperAction($f3, array $args = []) { | |
$Location_Mapper = new \app\Mapper\Location_Mapper($f3->get('DB')); | |
$Location_Mapper->name = 'test_mapper'; | |
$Location_Mapper->position = "ST_GeomFromText('POINT(4 4)')"; | |
$Location_Mapper->save(); | |
echo json_encode($Location_Mapper->cast()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment