Note that there are some explanatory texts on larger screens.

plurals
  1. POYii Framework handling Geospatial data
    text
    copied!<p>I am new to the Yii Framework and I have a database table that stores locations as Geospatial point objects. I am using the code generation tools to generate forms that update/ add to the database. However, these forms don't handle the location field properly. I have been struggling to figure out how this works. The solution mentioned <a href="https://stackoverflow.com/questions/12535245/how-do-i-insert-a-mysql-spatial-point-with-a-yii-model">in this post</a> does not help.</p> <p>I would appreciate it if anyone could link me to a tutorial on how to handle geospatial point objects within a yii model. I feel like this must be a really easy solution but I cannot seem to get my head around it. The source code for my model, form and controller are provided below:</p> <p>Model</p> <pre><code>&lt;?php /** * This is the model class for table "store". * * The followings are the available columns in table 'store': * @property integer $id * @property string $name * @property string $location */ class Store extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return Store the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'store'; } /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('id, name, location', 'required'), array('id', 'numerical', 'integerOnly'=&gt;true), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, name, location', 'safe', 'on'=&gt;'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=&gt;label) */ public function attributeLabels() { return array( 'id' =&gt; 'ID', 'name' =&gt; 'Name', 'location' =&gt; 'Location', ); } /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; $criteria-&gt;compare('id',$this-&gt;id); $criteria-&gt;compare('name',$this-&gt;name,true); $criteria-&gt;compare('location',$this-&gt;location,true); return new CActiveDataProvider($this, array( 'criteria'=&gt;$criteria, )); } } </code></pre> <p>Controller</p> <pre><code>&lt;?php class StoreController extends Controller { /** * @var string the default layout for the views. Defaults to '//layouts/column2', meaning * using two-column layout. See 'protected/views/layouts/column2.php'. */ public $layout='//layouts/column2'; /** * @return array action filters */ public function filters() { return array( 'accessControl', // perform access control for CRUD operations 'postOnly + delete', // we only allow deletion via POST request ); } /** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=&gt;array('index','view'), 'users'=&gt;array('*'), ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=&gt;array('create','update'), 'users'=&gt;array('@'), ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=&gt;array('admin','delete'), 'users'=&gt;array('admin'), ), array('deny', // deny all users 'users'=&gt;array('*'), ), ); } /** * Displays a particular model. * @param integer $id the ID of the model to be displayed */ public function actionView($id) { $this-&gt;render('view',array( 'model'=&gt;$this-&gt;loadModel($id), )); } /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model=new Store; // Uncomment the following line if AJAX validation is needed // $this-&gt;performAjaxValidation($model); if(isset($_POST['Store'])) { $model-&gt;attributes=$_POST['Store']; if($model-&gt;save()) $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id)); } $this-&gt;render('create',array( 'model'=&gt;$model, )); } /** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model=$this-&gt;loadModel($id); // Uncomment the following line if AJAX validation is needed // $this-&gt;performAjaxValidation($model); if(isset($_POST['Store'])) { $model-&gt;attributes=$_POST['Store']; if($model-&gt;save()) $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id)); } $this-&gt;render('update',array( 'model'=&gt;$model, )); } /** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { $this-&gt;loadModel($id)-&gt;delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if(!isset($_GET['ajax'])) $this-&gt;redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } /** * Lists all models. */ public function actionIndex() { $dataProvider=new CActiveDataProvider('Store'); $this-&gt;render('index',array( 'dataProvider'=&gt;$dataProvider, )); } /** * Manages all models. */ public function actionAdmin() { $model=new Store('search'); $model-&gt;unsetAttributes(); // clear any default values if(isset($_GET['Store'])) $model-&gt;attributes=$_GET['Store']; $this-&gt;render('admin',array( 'model'=&gt;$model, )); } /** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model=Store::model()-&gt;findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } /** * Performs the AJAX validation. * @param CModel the model to be validated */ protected function performAjaxValidation($model) { if(isset($_POST['ajax']) &amp;&amp; $_POST['ajax']==='store-form') { echo CActiveForm::validate($model); Yii::app()-&gt;end(); } } } </code></pre> <p>form:</p> <pre><code>&lt;?php /* @var $this StoreController */ /* @var $model Store */ /* @var $form CActiveForm */ ?&gt; &lt;div class="form"&gt; &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'store-form', 'enableAjaxValidation'=&gt;false, )); ?&gt; &lt;p class="note"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt; &lt;?php echo $form-&gt;errorSummary($model); ?&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'id'); ?&gt; &lt;?php echo $form-&gt;textField($model,'id'); ?&gt; &lt;?php echo $form-&gt;error($model,'id'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'name'); ?&gt; &lt;?php echo $form-&gt;textArea($model,'name',array('rows'=&gt;6, 'cols'=&gt;50)); ?&gt; &lt;?php echo $form-&gt;error($model,'name'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'location'); ?&gt; &lt;?php echo $form-&gt;textField($model,'location'); ?&gt; &lt;?php echo $form-&gt;error($model,'location'); ?&gt; &lt;/div&gt; &lt;div class="row buttons"&gt; &lt;?php echo CHtml::submitButton($model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt; &lt;/div&gt; &lt;?php $this-&gt;endWidget(); ?&gt; &lt;/div&gt;&lt;!-- form --&gt; </code></pre> <p>This is a screenshot of what happens when I try to add row to the store table from the auto generated form:</p> <p><img src="https://i.stack.imgur.com/Owp2b.png" alt="error"></p> <p>This is expected as a point object has to be constructed from the latitude and longitude. I do not know how to incorporate this into the Yii model. Thanks in advance for any help provided!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload