PHP code example of openbuildings / jam-locations

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

    

openbuildings / jam-locations example snippets


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)

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'),
			));
	}
}

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"