Mini Shell
<?php
/**
* Model
*
* @author Andrew Esteves <andrew@digitalnativa.com.br>
* @copyright Digital Nativa (www.digitalnativa.com.br)
*/
abstract class Model
{
/**
* Represents the model's table
* @var String $table
*/
protected $table;
/**
* Limit of records per page
* @var Int
*/
public $limit = 10;
/**
* Database connection
* @var Object
*/
protected $connection;
/**
* Initialize
*/
public function __construct()
{
$this->connection = DB::connection();
}
/**
* Find all data from $table ordered by $id desc
*
* @param string orderBy
* @param string order
* @param int limit
* @return Object
*/
public function findAll($orderBy = 'id', $order = 'desc', $limit = null)
{
$order = strtolower($order);
$sql = "SELECT * FROM {$this->table} ORDER BY $orderBy $order ";
$sql .= $limit ? "LIMIT " . $limit . " " : "";
try {
$stmt = $this->connection->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Find a record from $table by given the $id
*
* @param Int $id
* @return Object
*/
public function find($id)
{
$sql = "SELECT * FROM $this->table WHERE id = :id";
try {
$stmt = $this->connection->prepare($sql);
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Create record into the $table
*
* @param Array $data
* @return Boolean
*/
public function create($data){
$created = $updated = date('Y-m-d H:i:s');
$sql = "INSERT INTO {$this->table} SET ";
foreach($data as $argk => $argv) {
$sql .= $argk . " = :" . $argk . ", ";
}
$sql .= "created = :created, updated = :updated ";
try {
$stmt = $this->connection->prepare($sql);
foreach($data as $argk => &$argv) {
$stmt->bindParam($argk, $argv);
}
$stmt->bindParam(':created', $created);
$stmt->bindParam(':updated', $updated);
return $stmt->execute();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Update data from $id in $table
*
* @param Int $id
* @param Array $data
* @return Boolean
*/
public function update($id, $data)
{
$updated = date('Y-m-d H:i:s');
$sql = "UPDATE {$this->table} SET ";
foreach($data as $argk => $argv) {
$sql .= $argk . " = :" . $argk . ", ";
}
$sql .= " updated = :updated ";
$sql .= "WHERE id = :id";
try {
$stmt = $this->connection->prepare($sql);
foreach($data as $argk => &$argv) {
$stmt->bindParam($argk, $argv);
}
$stmt->bindParam(':updated', $updated);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Delete record from $table
*
* @param Int id
* @return Boolean
*/
public function delete($id)
{
$sql = "DELETE FROM {$this->table} WHERE id = :id";
try {
$stmt = $this->connection->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Paginate records from $table
*
* @param int offset
* @param string orderBy
* @param string order
* @return Object
*/
public function paginate($offset = 0, $orderBy = 'id', $order = 'desc')
{
$offset = ($offset * $this->limit) - $this->limit;
$sql = "SELECT * FROM {$this->table} ORDER BY $orderBy " . strtoupper($order) . " LIMIT $offset, {$this->limit}";
try {
$stmt = $this->connection->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Total records from $table
*
* @return Int
*/
public function total()
{
$sql = "SELECT COUNT(*) FROM {$this->table}";
try {
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$count = $stmt->fetch();
$result = get_object_vars($count);
return $result['COUNT(*)'];
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
/**
* Query data with given condition
*
* @param Array $args
* @param String $cond
* @param Int $limit
* @return Object
*/
public function condition($args, $cond = 'and', $limit = 0)
{
$sql = "SELECT * FROM {$this->table} WHERE ";
$cond = strtoupper($cond);
$argsSize = count($args);
$n = 1;
foreach($args as $argk => $argv) {
$sql .= $argk . " = :" . $argk;
if($n < $argsSize) {
$sql .= " {$cond} ";
}
$n++;
}
if($limit) $sql .= " LIMIT {$limit}";
try {
$stmt = $this->connection->prepare($sql);
foreach($args as $argk => &$argv) {
$stmt->bindParam($argk, $argv);
}
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
}
Zerion Mini Shell 1.0