pigochu / yii2-jquery-locationpicker

jquery location picker widget for yii2
20 stars 7 forks source link

CoordinatesPicker fix for virtual attributes #15

Closed karvaka closed 6 years ago

karvaka commented 6 years ago

I had some problems using ActiveRecord virtual attributes with CoordinatesPicker. This pull request enables usage of this feature and allows make things like following:

<?php
namespace common\models;

/**
 * Table schema attributes.
 * @property double $latitude
 * @property double $longitude
 *
 * Overloaded attribute.
 * @property string|null $coordinates
 */
class SomePlace extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['coordinates', 'required'],
            ['coordinates', 'string']
        ];
    }

    /**
     * @return null|string
     */
    public function getCoordinates()
    {
        if (!$this->latitude || !$this->longitude) {
            return null;
        }
        return $this->latitude . ',' . $this->longitude;
    }

    /**
     * @param $coordinates
     */
    public function setCoordinates($coordinates)
    {
        $parts = explode(',', $coordinates);
        if (count($parts) === 2) {
            list($this->latitude, $this->longitude) = $parts;
        }
    }
}

This approach seems very efficient for me. Much easier to integrate in comparison with this one https://github.com/pigochu/yii2-jquery-locationpicker/blob/master/doc/TWO-FIELDS-CONVERSION.md. Thanks.

pigochu commented 6 years ago

Could you pull to branch dev again ? I will test it . Thank you.