<?php

namespace Jaga\Models;

use Phalcon\Mvc\Model;
use Jaga\Traits\ThrowableModel;
//use Phalcon\Mvc\Model\Behavior\Timestampable;

class BaseModel extends Model
{
    use ThrowableModel;

    public function onConstruct()
    {
        // do anything here

        return true;
    }

    public function toArray($columns = null)
    {
        $result = parent::toArray($columns);

        return $result;
    }

    public function isNewRecord()
    {
    	return (isset($this->id) && $this->id != null) ? false : true;
    }

    public function store($data = NULL, $whiteList = NULL)
    {
        return $this->isNewRecord ? parent::create($data, $whiteList) : parent::update($data, $whiteList);
    }

    public static function firstOrCreate($params)
    {
        $model = parent::findFirst($params);

        return empty($model) ? new static() : $model;
    }

    public static function getAsReference($key) {
        $rows = self::find();
        $refs = [];
        foreach ($rows->toArray() as $row) {
            $refs[$row[$key]] = $row;
        }

        return $refs;
    }
}
