<?php

namespace Modules\AdministrativeUnits\Models;

use CodeIgniter\Model;

class CountyModel extends Model
{
    private $tbl_district = "districts";
    public function __construct()
    {
        parent::__construct();

        $this->table = "counties";
        $this->primaryKey = "county_id";
       
    }
    
    protected function initialize()
    {
        

        $this->validationRules    = [
            'county_name'     => 'required|alpha_numeric_space|min_length[3]',
            
        ];

        
        $this->allowedFields = ['county_name','district_id'];
    }
	
	public function findByName($name,$districtId)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->table}.county_name",$name);
		$builder->where("{$this->table}.district_id",$districtId);
        return $builder->get()->getRowArray();
    }

    /**public function findAll(int $limit = 0, int $offset = 0)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        if($limit > 0){
            $builder->limit($limit, $offset);
        }
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->table}.district_id");
        return $builder->get()->getResultArray();
    }**/

    public function findByDistrictId($id)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*, district_name');
        $builder->where("{$this->tbl_district}.district_id",$id);
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->table}.district_id");
        return $builder->get()->getResultArray();
    }
}