openbuildings/jam-locations
Composer 安装命令:
composer require openbuildings/jam-locations
包简介
Geoip and IP integration
README 文档
README
This module adds hierarchical locations (continents, countries, cities), by leveraging openbuildings/jam-closuretable. As well as auto initialized ip field and countries / cities set automatically through geolocation.
Usage
Using ip field is as easy as just defining it:
class Model_User extends Jam_Model { public static function initialize(Jam_Meta $meta) { $meta ->fields(array( // ... 'ip' => Jam::field('ip'), )); } } $user = Jam::build('user'); echo $user->ip; // will return the current ip address (from Request::$clent_ip)
Auto Location is a bit more involved:
class Model_User extends Jam_Model { public static function initialize(Jam_Meta $meta) { $meta ->behaviors(array( 'location_auto' => Jam::behavior('location_auto', array( 'locations' => array( // association => geoip record name 'city' => 'city', 'country' => 'country_name', ) )), )) ->associations(array( 'country' => Jam::association('belongsto', array('foreign_model' => 'location')), 'city' => Jam::association('belongsto', array('foreign_model' => 'location')), )) ->fields(array( 'id' => Jam::field('primary'), 'ip' => Jam::field('ip'), )); } }
Then if you do not set city or country association, it would use the ip field to get a geoip record, and from there try to find / create the appropriate location.
Lastly the location_parent behavior is used to assign a parent to a location association if both the child and the parent are present in the model:
class Model_Address extends Jam_Model { public static function initialize(Jam_Meta $meta) { $meta ->behaviors(array( 'location_parent' => Jam::behavior('location_parent', array( 'parents' => array( // child => parent 'city' => 'country', ) )), )) ->associations(array( 'country' => Jam::association('belongsto', array('foreign_model' => 'location')), 'city' => Jam::association('belongsto', array('foreign_model' => 'location')), )) ->fields(array( 'id' => Jam::field('primary'), )); } } $address = Jam::build('address'); $address->country = Jam::find('location', 'France'); $address->city = Jam::build('location', array('name' => 'Paris')); $address->save(); echo $address->city->parent->name(); // will return "France"
Requirement
This module requires php geoip extension.
License
Copyright (c) 2012-2013, OpenBuildings Ltd. Developed by Ivan Kerin as part of clippings.com
Under BSD-3-Clause license, read LICENSE file.
统计信息
- 总下载量: 170.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2013-09-25