PHPackages                             reza-id/yii2-geopoint - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. reza-id/yii2-geopoint

ActiveYii2-extension[Database &amp; ORM](/categories/database)

reza-id/yii2-geopoint
=====================

Yii2 ActiveRecord supporting MySQL spatial pint data

v1.1.0(8y ago)52.1k3[1 PRs](https://github.com/reza-id/yii2-geopoint/pulls)MITPHP

Since Sep 19Pushed 7y ago1 watchersCompare

[ Source](https://github.com/reza-id/yii2-geopoint)[ Packagist](https://packagist.org/packages/reza-id/yii2-geopoint)[ RSS](/packages/reza-id-yii2-geopoint/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Yii2-geopoint
=============

[](#yii2-geopoint)

ActiveRecord inspired by [yii2-spatial](https://github.com/sjaakp/yii2-spatial) but made simpler only to use specific spatial datatype: POINT.
Transform the internal [MySQL format](https://dev.mysql.com/doc/refman/5.5/en/spatial-datatypes.html) to simple coordinate text after finding, and vice versa before storing.

**Yii2-geopoint** can also be used to find the model or models which are nearest to a given location.

**Notice that this extension can be used with `MySQL >= 5.6.1`, `MariaDB >= 5.3.3`, and `PostgreSQL >= 9.1`.**

Installation
------------

[](#installation)

Install **Yii2-geopoint** with [Composer](https://getcomposer.org/). Either add the following to the require section of your `composer.json` file:

`"reza-id/yii2-geopoint": "*"`

Or run:

`$ php composer.phar require reza-id/yii2-geopoint "*"`

You can manually install **Yii2-geopoint** by [downloading the source in ZIP-format](https://github.com/reza-id/yii2-geopoint/archive/master.zip).

Usage
-----

[](#usage)

Create spatial indexed table using migration:

```
$this->createTable('{{%place}}', [
	'id' => $this->primaryKey(),
	'name' => $this->string(125)->notNull(),
	'location' => 'POINT NOT NULL',
], $tableOptions);

if ($this->db->driverName === 'mysql') {
	$this->execute('CREATE SPATIAL INDEX `idx-place-location` ON '.'{{%place}}(location);');
} elseif ($this->db->driverName === 'pgsql') {
	$this->execute('CREATE INDEX "idx-place-location" ON '.'{{%place}} USING GIST(location);');
}

```

Use a `rezaid\geopoint\ActiveRecord` as base class for your models, like so:

```
