Skip to content

Instantly share code, notes, and snippets.

@eskrano
Created September 23, 2015 15:18
Show Gist options
  • Save eskrano/a5d3e69f39d0eda3b193 to your computer and use it in GitHub Desktop.
Save eskrano/a5d3e69f39d0eda3b193 to your computer and use it in GitHub Desktop.
<?php
namespace App\System\Model;
use App\System\Database\Connector;
Class SimpleModel
{
public $table;
public $query_builder_cache;
public $query_builder_cache_placeholder = [];
public $cache_insert_keys = [];
public $cache_insert_values = [];
static public $i;
public static function __callStatic($method,$argments)
{
if (self::$i == null)
{
self::$i = new self();
}
return var_dump(self::$i);
}
public function setTable($table)
{
$this->table = $table;
}
public function __construct()
{
$this->database = Connector::getInstanse();
$this->db = Connector::getInstanse()->db;
}
public function baseQuery($sql,...$placeholders)
{
$result = $this->db->prepare($sql);
$result->execute(($placeholders != null ? $placeholders : $addPlaceholders));
return $result;
}
public function get($result)
{
return (object) $result->fetchAll(\PDO::FETCH_ASSOC);
}
public function first($result)
{
return (object) $result->fetch(\PDO::FETCH_ASSOC);
}
public function count($result)
{
return (int) $result->rowCount();
}
public function notPrepare($sql)
{
$result = $this->db->exec($sql);
return $result;
}
public function select(...$columns)
{
if (is_array($columns))
{
$columns = implode(',', $columns);
}
if ($columns == null)
{
$columns = '*';
}
$this->query_builder_cache .= 'SELECT '.$columns.' ';
return $this;
}
public function from($table = null)
{
$this->query_builder_cache .= 'FROM `'.(isset($table) ? $table : $this->table).'`';
return $this;
}
public function where($column,$type,$value)
{
$this->query_builder_cache .= ' WHERE '.$column.$type.'?';
$this->query_builder_cache_placeholder[] = $value;
return $this;
}
public function orWhere($column,$type,$value)
{
$this->query_builder_cache .= 'OR '.$column.$type.'?';
$this->query_builder_cache_placeholder[] = $value;
return $this;
}
public function andWhere($column,$type,$value)
{
$this->query_builder_cache .= ' && '.$column.$type.'?';
$this->query_builder_cache_placeholder[] = $value;
return $this;
}
public function getOne()
{
$r = $this->baseBuilderQuery($this->query_builder_cache,$this->query_builder_cache_placeholder);
$this->flushCache();
return $this->first($r);
}
public function getAll()
{
$r = $this->baseBuilderQuery($this->query_builder_cache,$this->query_builder_cache_placeholder);
$this->flushCache();
return $this->get($r);
}
public function insert($data)
{
$this->extractData($data);
$this->query_builder_cache = 'INSERT INTO `'.$this->table.'` ('.implode(',', $this->cache_insert_keys) . ') VALUES ('.$this->buildValuesString().')';
$this->baseBuilderQuery($this->query_builder_cache,$this->cache_insert_values);
var_dump(extract($this->cache_insert_values));
}
protected function baseBuilderQuery($sql,$params)
{
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
if (!$stmt)
{
throw new Exception("Bad sql for insert data");
}
return $stmt;
}
protected function extractData($data)
{
foreach ($data as $k=>$v)
{
$this->cache_insert_keys[] = $k;
$this->cache_insert_values[] = $v;
}
}
public function flushCache()
{
$this->query_builder_cache_placeholder = null;
$this->query_builder_cache = null;
}
public function buildValuesString()
{
$replacement = array_values($this->cache_insert_values);
$return = [];
for ($i = 0; $i<count($replacement); $i++)
{
$return[] = '?';
}
return implode(',',$return);
}
}
<?php
namespace App\Models;
use App\System\Model\SimpleModel;
Class Table extends SimpleModel
{
public $table = 'table';
}
<?php
namespace App\Controllers;
use App\Models\Table;
use App\System\Controller;
Class Welcome extends Controller
{
public function index()
{
$Table = new Table;
var_dump(
$Table->select()
->from()
->where('name','=','Alex')
->orWhere('name','=','Саша')
->getAll()
);
return $this->display('<br>Hello world!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment