<?php

class MasterKotaKabupaten extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var string
     */
    public $kota_kabupaten_code;

    /**
     *
     * @var string
     */
    public $kota_kabupaten_name;

    /**
     *
     * @var string
     */
    public $provinsi_code;

    /**
     *
     * @var string
     */
    public $created_at;

    /**
     *
     * @var string
     */
    public $updated_at;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("master_kota_kabupaten");
        $this->hasMany('kota_kabupaten_code', 'MasterKecamatan', 'kota_kabupaten_code', ['alias' => 'MasterKecamatan']);
        $this->belongsTo('provinsi_code', '\MasterProvinsi', 'provinsi_code', ['alias' => 'MasterProvinsi']);
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'master_kota_kabupaten';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return MasterKotaKabupaten[]|MasterKotaKabupaten|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return MasterKotaKabupaten|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        if (is_numeric($parameters)) {
            return parent::findFirst(([
                "conditions" => "kota_kabupaten_code = :code:",
                "bind" => [
                    "code" => strval($parameters)
                ]
            ]));
        }
        return parent::findFirst($parameters);
    }

    public function beforeCreate()
    {
        $this->created_at = date("Y-m-d H:i:s");
        $this->updated_at = date("Y-m-d H:i:s");
    }

    public function beforeUpdate()
    {
        $this->updated_at = date("Y-m-d H:i:s");
    }

    public static function getAsReference()
    {
        $kotaKabupatens = MasterKotaKabupaten::find();
        $refKotakabupaten = [];
        foreach ($kotaKabupatens as $kotaKabupaten) {
            $refKotakabupaten[$kotaKabupaten->kota_kabupaten_code] = $kotaKabupaten;
        }
        return $refKotakabupaten;
    }

}
