PHP code example of yii2-extensions / postgis

1. Go to this page and download the library: Download yii2-extensions/postgis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii2-extensions / postgis example snippets




use yii\db\ActiveRecord;
use Yii2\Extension\Postgis\Behaviors\GeometryBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::class,
                'type' => GeometryBehavior::GEOMETRY_POINT,
                'attribute' => 'point',
                // explicitly set custom db connection if you do not want to use
                // static::getDb() or Yii::$app->getDb() connections
                'db' => 'db_custom'
            ],
            [
                'class' => GeometryBehavior::class,
                'type' => GeometryBehavior::GEOMETRY_LINESTRING,
                'attribute' => 'line',
                // skip attribute if it was not selected as Geo Json (by PostgisQueryTrait), because it 



use yii\db\ActiveRecord;
use Yii2\Extension\Postgis\Behaviors\GeometryBehavior;
use Yii2\Extension\Postgis\Behaviors\StBufferBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::className(),
                'attribute' => 'point',
                'type' => GeometryBehavior::GEOMETRY_POINT,
            ],
            [
                'class' => StBufferBehavior::className(),
                'attribute' => 'buffer',
                'attributeGeometry' => 'point',
                'attributeRadius' => 'radius',
            ],
        ];
    }

    // ...

}

// ...

$model = new MyModel;

$model->point = [39.234, 54.456];
$model->radius = 5;

// It will be save St_Buffer for `$model->point` with `$model->radius` in `$model->buffer`
$model->save();




class MyQuery extends \yii\db\ActiveQuery
{
    use \Yii2\Extension\Postgis\Db\PostgisQueryTrait;
    
    // ...
}

// ...

class MyModel extends \yii\db\ActiveRecord
{
    public static function find(){
        return \Yii::createObject([
            'class' => MyQuery::className(),
        ], [get_called_class()]);
    }
}