<?php
class UnityModel extends MY_Model {
	protected $table = 'unities';
	protected $primary = 'id';
	protected $field_order = 'sort_position';
	protected $type_order = 'asc';

	public function getByCityName ($city) {
		if (!$city) return null;

		$limit = 1;

		$unity = $this->db
			->select('unities.*, unities.address_city AS near_city')
			->like('LOWER(unities.address_city)', strtolower(normalize($city)))
			->get($this->table, $limit)
			->row();

		if (!$unity) {	
			$unity = $this->db
			->select('unities.*, cities.name AS near_city')
			->like('LOWER(cities.name)', strtolower(normalize($city)))
			->join('unities', 'unities.id = cities.unity_id')
			->get('cities', $limit)
			->row();
		}
			
		return $unity;
	}
}
