簡體   English   中英

如何在CodeIgniter 2中擴展Doctrine 2來處理MySQL空間(“點”字段類型)?

[英]How can I extend Doctrine 2 to handle MySQL spatial (“point” field type) in CodeIgniter 2?

我正在使用CodeIgniter 2編寫一個網站作為我的框架,我最近開始嘗試將Doctrine 2用於ORM。

我的數據庫需要存儲空間數據(例如“點”類型的列),顯然Doctrine無法處理開箱即用的數據。

我在互聯網上搜索過,只發現了這些提示: http//codeutopia.net/blog/2011/02/19/using-spatial-data-in-doctrine-2/

但該文章尚不清楚保存這些文件的位置以及需要配置的其他內容。 我嘗試聯系作者,但從未收到過回復。

有人可以幫忙嗎?

謝謝! 瑞安

很久以前所以我忘記了一些細節,但是我將這4個文件放在一個名為CodeIgniter / application / libraries / Doctrine / CustomAddon的文件夾中:

  • Distance.php
  • Point.php
  • PointStr.php
  • PointType.php

我將每個命名空間設置為“CustomAddon”。

然后在CodeIgniter / application / libraries / Doctrine.php的底部我放了:

require_once APPPATH . 'libraries/Doctrine/CustomAddon/Distance.php';
require_once APPPATH . 'libraries/Doctrine/CustomAddon/Point.php';
require_once APPPATH . 'libraries/Doctrine/CustomAddon/PointStr.php';
require_once APPPATH . 'libraries/Doctrine/CustomAddon/PointType.php';
Doctrine\DBAL\Types\Type::addType('point', 'CustomAddon\PointType');
//Assuming $config is a Doctrine\ORM\Configuration object used to configure the EM
$config->addCustomNumericFunction('DISTANCE', 'CustomAddon\Distance');
$config->addCustomNumericFunction('POINT_STR', 'CustomAddon\PointStr');

// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);

用法看起來像這樣:

$point = new \CustomAddon\Point($geo['lat'], $geo['lon']);

我使用的是學說1.2,所以我不知道這是否適合你。 使用1.2.4,您可以在yaml或模型中聲明一個點類型,並且它可以很好地轉換為數據庫。 要將數據保存為Point,您需要創建一個新的學說表達式,如下所示:

$phone->point = new Doctrine_Expression("GEOMFROMTEXT('POINT(25 10)')");

當然,您需要的數據代替那些25和10。 使用doctrine 1.2.4,這將正常工作並將數據保存在數據庫中。 要檢索數據,您只需使用這種select語句:

$query = Doctrine_Query::create()
         ->select ('X(p.point), Y(p.point)')
         ->from('Points p');

並且您將在對象中有一個名為point的數組,其中包含兩個元素(x和y)。 正如我所說的,我使用1.2.4,但是如果有一種簡單的方法來處理該版本中的點,類似的東西必須與學說2一起工作。祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM