-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
838140f
commit a302ad7
Showing
4 changed files
with
255 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Accolon\DataLayer; | ||
|
||
use PDOException; | ||
use Accolon\DataLayer\Db; | ||
use Accolon\DataLayer\Traits\CRUD; | ||
use Accolon\DataLayer\Traits\Query; | ||
|
||
abstract class Model | ||
{ | ||
use Query, CRUD; | ||
|
||
private ?string $limit = ""; | ||
private ?string $columns = ""; | ||
private ?string $offset = ""; | ||
private ?string $order = ""; | ||
private ?string $statement = ""; | ||
private ?array $params = []; | ||
private ?int $operation = 0; | ||
private ?string $where = ""; | ||
|
||
public function persist($iterable): void | ||
{ | ||
foreach($iterable as $attr => $value) { | ||
$this->$attr = $value; | ||
} | ||
} | ||
|
||
public function clear() | ||
{ | ||
$attrs = ["limit", "offset", "columns", "order", "statement", "params", "operation", "where"]; | ||
foreach($attrs as $attr) { | ||
|
||
$this->$attr = null; | ||
} | ||
} | ||
|
||
public function limit(int $num): Model | ||
{ | ||
$this->limit = "LIMIT {$num} "; | ||
return $this; | ||
} | ||
|
||
public function offset(int $num): Model | ||
{ | ||
$this->offset = "OFFSET {$num} "; | ||
return $this; | ||
} | ||
|
||
public function order(string $col, string $order): Model | ||
{ | ||
$this->order = "ORDER BY {$col} {$order} "; | ||
return $this; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
$this->select(); | ||
|
||
$this->operation = Operation::Count; | ||
|
||
return $this->execute(); | ||
} | ||
|
||
public function addSelect(string $col): Model | ||
{ | ||
$this->columns .= ", {$col}"; | ||
|
||
$this->statement = "SELECT {$this->columns} FROM {$this->table} "; | ||
|
||
return $this; | ||
} | ||
|
||
public function execute(bool $all = true) | ||
{ | ||
try{ | ||
$db = Db::connection(); | ||
|
||
$db->beginTransaction(); | ||
|
||
$stmt = $db->prepare( | ||
$this->statement . $this->where . $this->order . $this->limit . $this->offset | ||
); | ||
|
||
$result = $stmt->execute($this->params); | ||
|
||
$db->commit(); | ||
|
||
if(!$stmt->rowCount()){ | ||
return null; | ||
} | ||
|
||
if($this->operation == Operation::Count) { | ||
return $stmt->rowCount(); | ||
} | ||
|
||
if ($this->operation == Operation::Insert) { | ||
$this->id = $db->lastInsertId(); | ||
} | ||
|
||
if($this->operation != Operation::Select){ | ||
return $result; | ||
} | ||
|
||
$this->clear(); | ||
|
||
if($all){ | ||
return $stmt->fetchAll(); | ||
} | ||
|
||
$result = $stmt->fetchObject(); | ||
|
||
$obj = new $this(); | ||
|
||
$obj->persist($result); | ||
|
||
$obj->table = $this->table; | ||
|
||
return $obj; | ||
|
||
}catch(PDOException $e){ | ||
// echo $this->statement . $this->where . $this->order . $this->limit . $this->offset; // Debug | ||
$db->rollBack(); | ||
die("Error: {$e->getMessage()}"); | ||
} | ||
} | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Accolon\DataLayer; | ||
|
||
use PDOException; | ||
use Accolon\DataLayer\Db; | ||
use Accolon\DataLayer\Traits\CRUD; | ||
use Accolon\DataLayer\Traits\Query; | ||
|
||
abstract class Model | ||
{ | ||
use Query, CRUD; | ||
|
||
private ?string $limit = ""; | ||
private ?string $columns = ""; | ||
private ?string $offset = ""; | ||
private ?string $order = ""; | ||
private ?string $statement = ""; | ||
private ?array $params = []; | ||
private ?int $operation = 0; | ||
private ?string $where = ""; | ||
|
||
public function persist($iterable): void | ||
{ | ||
foreach($iterable as $attr => $value) { | ||
$this->$attr = $value; | ||
} | ||
} | ||
|
||
public function clear() | ||
{ | ||
$attrs = ["limit", "offset", "columns", "order", "statement", "params", "operation", "where"]; | ||
foreach($attrs as $attr) { | ||
$this->$attr = null; | ||
} | ||
} | ||
|
||
public function limit(int $num): Model | ||
{ | ||
$this->limit = "LIMIT {$num} "; | ||
return $this; | ||
} | ||
|
||
public function offset(int $num): Model | ||
{ | ||
$this->offset = "OFFSET {$num} "; | ||
return $this; | ||
} | ||
|
||
public function order(string $col, string $order): Model | ||
{ | ||
$this->order = "ORDER BY {$col} {$order} "; | ||
return $this; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
$this->select(); | ||
|
||
$this->operation = Operation::Count; | ||
|
||
return $this->execute(); | ||
} | ||
|
||
public function addSelect(string $col): Model | ||
{ | ||
$this->columns .= ", {$col}"; | ||
|
||
$this->statement = "SELECT {$this->columns} FROM {$this->table} "; | ||
|
||
return $this; | ||
} | ||
|
||
public function execute(bool $all = true) | ||
{ | ||
try{ | ||
$db = Db::connection(); | ||
|
||
$db->beginTransaction(); | ||
|
||
$stmt = $db->prepare( | ||
$this->statement . $this->where . $this->order . $this->limit . $this->offset | ||
); | ||
|
||
$result = $stmt->execute($this->params); | ||
|
||
$db->commit(); | ||
|
||
if(!$stmt->rowCount()){ | ||
return null; | ||
} | ||
|
||
if($this->operation == Operation::Count) { | ||
return $stmt->rowCount(); | ||
} | ||
|
||
if ($this->operation == Operation::Insert) { | ||
$this->id = $db->lastInsertId(); | ||
} | ||
|
||
if($this->operation != Operation::Select){ | ||
return $result; | ||
} | ||
|
||
$this->clear(); | ||
|
||
if($all){ | ||
return $stmt->fetchAll(); | ||
} | ||
|
||
$result = $stmt->fetchObject(); | ||
|
||
$obj = new $this(); | ||
|
||
$obj->persist($result); | ||
|
||
$obj->table = $this->table; | ||
|
||
return $obj; | ||
|
||
}catch(PDOException $e){ | ||
// echo $this->statement . $this->where . $this->order . $this->limit . $this->offset; // Debug | ||
$db->rollBack(); | ||
die("Error: {$e->getMessage()}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.