Advertisement
Qamaruddin

GIS Model

May 9th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\models;
  4.  
  5. use Yii;
  6.  
  7. /**
  8.  * This is the model class for table "places_gis".
  9.  *
  10.  * @property integer $id
  11.  * @property integer $place_id
  12.  * @property string $location
  13.  * @property string $created_at
  14.  * @property string $updated_at
  15.  */
  16. class PlacesGis extends \common\models\_BaseModel {
  17.  
  18.     /**
  19.      * @inheritdoc
  20.      */
  21.     public static function tableName() {
  22.         return 'places_gis';
  23.     }
  24.  
  25.     /**
  26.      * @inheritdoc
  27.      */
  28.     public function rules() {
  29.         return [
  30.             [['place_id'], 'integer'],
  31.             [['location', 'created_at', 'updated_at'], 'required'],
  32.             [['location'], 'string'],
  33.             [['created_at', 'updated_at'], 'safe']
  34.         ];
  35.     }
  36.  
  37.     /**
  38.      * @inheritdoc
  39.      */
  40.     public function attributeLabels() {
  41.         return [
  42.             'id' => Yii::t('app', 'ID'),
  43.             'place_id' => Yii::t('app', 'Place ID'),
  44.             'location' => Yii::t('app', 'Location'),
  45.             'created_at' => Yii::t('app', 'Created At'),
  46.             'updated_at' => Yii::t('app', 'Updated At'),
  47.         ];
  48.     }
  49.  
  50.     /**
  51.      * @inheritdoc
  52.      * @return PlacesGisQuery the active query used by this AR class.
  53.      */
  54.     public static function find() {
  55.         return new PlacesGisQuery(get_called_class());
  56.     }
  57.  
  58.     /**
  59.      * since yii2 does not support MYSQL GIS Out of the box
  60.      * @param float $long x
  61.      * @param float $lat y
  62.      * @param integer $place_id
  63.      * @return Boolean
  64.      */
  65.     public function saveRaw($long, $lat, $place_id) {
  66.  
  67.         $sql = "INSERT INTO `" . self::tableName() . "` ("
  68.                 . "`place_id`, "
  69.                 . "`location`, "
  70.                 . "`created_at`, "
  71.                 . "`updated_at`"
  72.                 . ") VALUES (:param_placeid, GeomFromText(:point), TIMESTAMP(NOW()), TIMESTAMP(NOW()));";
  73.  
  74.         $command = \yii::$app->db->createCommand($sql, [
  75.             ':param_placeid' => $place_id,
  76.             ':point' => sprintf('POINT(%f %f)', $long, $lat)
  77.         ]);
  78.  
  79.         return $command->execute();
  80.     }
  81.  
  82.     /**
  83.      * @return \yii\db\ActiveQuery
  84.      */
  85.     public function getPlace() {
  86.         return $this->hasOne(Places::className(), ['id' => 'place_id']);
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement