Created
March 3, 2015 15:48
-
-
Save damianoporta/f37483aeab453704717e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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