<?php
namespace startmvc\core;
use startmvc\core\Db;
abstract class Model
{
protected $table;
protected $db;
protected $dbConf;
public function __construct ()
{
$this->dbConf = include CONFIG_PATH . '/database.php';
if ($this->dbConf['default'] != '') {
$this->db= new Db($this->dbConf['connections'][$this->dbConf['default']]);
}
}
public function find($field="*",$where='',$getsql=false)
{
$res=self::findAll($field,$where,'',1,$getsql);
if($res){
return $res[0];
}
}
public function findAll($field="*",$where=[],$order='',$limit='',$getsql=false)
{
$prefix=$this->dbConf['connections'][$this->dbConf['default']]['prefix'];
$this->db->select($field);
$this->db->table($this->table);
if (!empty($where)) {
$this->db->where($where);
}
if($order){
$this->db->order($order);
}
if ($limit){
if(is_numeric($limit)){
$this->db->limit($limit);
}else{
$limit_arr=explode(',',$limit);
$this->db->limit($limit_arr[0],$limit_arr[1]);
}
}
return $this->db->get($getsql);
}
public function update($data,$where=[])
{
$this->db->table($this->table);
if ($where){
$this->db->where($where);
}
return $this->db->update($data);
}
public function delete($where='')
{
$this->db->table($this->table);
if ($where){
$where=!is_numeric($where)?:['id'=>$where];
$where=$this->db->where($where);
}
return $this->db->delete();
}
}