<?php

class MasterProvinsi extends \Phalcon\Mvc\Model
{

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

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

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

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

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("master_provinsi");
        $this->hasMany('provinsi_code', 'MasterKotaKabupaten', 'provinsi_code', ['alias' => 'MasterKotaKabupaten']);
    }

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

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return MasterProvinsi[]|MasterProvinsi|\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 MasterProvinsi|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        if (is_numeric($parameters)) {
            return parent::findFirst(([
                "conditions" => "provinsi_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()
    {
        $provinsis = MasterProvinsi::find();
        $refProvinsi = [];
        foreach ($provinsis as $provinsi) {
            $refProvinsi[$provinsi->provinsi_code] = $provinsi;
        }
        return $refProvinsi;
    }

}
