Skip to content

Instantly share code, notes, and snippets.

@damianoporta
Created March 3, 2015 15:48
Show Gist options
  • Save damianoporta/f37483aeab453704717e to your computer and use it in GitHub Desktop.
Save damianoporta/f37483aeab453704717e to your computer and use it in GitHub Desktop.
<?php
public function zipcodeToRegion($zipcode, $zipcodeField = 'id', $regionField = 'id')
{
$result = $this
->find()
->select($regionField)
->join([
[
'table' => 'provinces',
'type' => 'INNER',
'conditions' => 'provinces.region_id = Regions.id',
],
[
'table' => 'cities',
'type' => 'INNER',
'conditions' => 'cities.province_id = provinces.id',
],
[
'table' => 'zipcodes',
'type' => 'INNER',
'conditions' => [
'zipcodes.city_id = cities.id',
"zipcodes.$zipcodeField" => $zipcode
]
]
])
->first();
if ($result)
{
return $result->$regionField;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment