PHP code example of nanson / yii2-postgis

1. Go to this page and download the library: Download nanson/yii2-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/ */

    

nanson / yii2-postgis example snippets




use yii\db\ActiveRecord;
use nanson\postgis\behaviors\GeometryBehavior;

class MyModel extends ActiveRecord
{

    // ...
    
    public function behaviors()
    {
        return [
            [
                'class' => GeometryBehavior::className(),
                '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::className(),
                '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 nanson\postgis\behaviors\GeometryBehavior;
use nanson\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 \nanson\postgis\db\PostgisQueryTrait;
    
    // ...
}

// ...

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