refactor(Model): Standarize property names

This commit is contained in:
2026-03-11 18:22:16 -03:00
parent c5a4967ae4
commit 76a69e5ef8

View File

@@ -26,37 +26,38 @@ class Model
/**
* @var array Attributes that should be set to NULL on update.
*/
protected array $toNull = [];
protected array $dbSetToNull = [];
/**
* @var string The name of the primary key column.
*/
protected static string $primaryKey = 'id';
protected static string $dbPrimaryKey = 'id';
/**
* @var array Attributes to ignore when saving to the database.
*/
protected static array $ignoreSave = ['id'];
protected static array $dbIgnoreSave = ['id'];
/**
* @var array Attributes that should be explicitly saved, even if private/protected.
*/
protected static array $forceSave = [];
protected static array $dbForceSave = [];
/**
* @var string The database table name.
*/
protected static string $table;
protected static string $dbTableName;
/**
* @var array Variables for PDO prepared statements.
*/
protected static array $queryVars = [];
protected static array $dbQueryVariables = [];
/**
* @var array Current SELECT query components.
*/
protected static array $querySelect = [
protected static array $dbQuery = [
'select' => ['*'],
'where' => '',
'from' => '',
@@ -156,13 +157,13 @@ class Model
try {
$prepared = $db->prepare($query);
$prepared->execute(static::$queryVars);
$prepared->execute(static::$dbQueryVariables);
} catch (PDOException $e) {
if ($db->inTransaction()) {
$db->rollBack();
}
$vars = json_encode(static::$queryVars);
$vars = json_encode(static::$dbQueryVariables);
throw new Exception(
"\nError at query to database.\n" .
@@ -187,7 +188,7 @@ class Model
*/
protected static function resetQuery(): void
{
static::$querySelect = [
static::$dbQuery = [
'select' => ['*'],
'where' => '',
'from' => '',
@@ -199,7 +200,7 @@ class Model
'groupBy' => '',
'limit' => '',
];
static::$queryVars = [];
static::$dbQueryVariables = [];
}
/**
@@ -211,44 +212,44 @@ class Model
*/
protected static function buildQuery(): string
{
$sql = 'SELECT ' . join(', ', static::$querySelect['select']);
$sql = 'SELECT ' . join(', ', static::$dbQuery['select']);
if (static::$querySelect['from'] != '') {
$sql .= ' FROM ' . static::$querySelect['from'];
if (static::$dbQuery['from'] != '') {
$sql .= ' FROM ' . static::$dbQuery['from'];
} else {
$sql .= ' FROM ' . static::table();
}
if (static::$querySelect['crossJoin'] != '') {
$sql .= static::$querySelect['crossJoin'];
if (static::$dbQuery['crossJoin'] != '') {
$sql .= static::$dbQuery['crossJoin'];
}
if (static::$querySelect['innerJoin'] != '') {
$sql .= static::$querySelect['innerJoin'];
if (static::$dbQuery['innerJoin'] != '') {
$sql .= static::$dbQuery['innerJoin'];
}
if (static::$querySelect['leftJoin'] != '') {
$sql .= static::$querySelect['leftJoin'];
if (static::$dbQuery['leftJoin'] != '') {
$sql .= static::$dbQuery['leftJoin'];
}
if (static::$querySelect['rightJoin'] != '') {
$sql .= static::$querySelect['rightJoin'];
if (static::$dbQuery['rightJoin'] != '') {
$sql .= static::$dbQuery['rightJoin'];
}
if (static::$querySelect['where'] != '') {
$sql .= ' WHERE ' . static::$querySelect['where'];
if (static::$dbQuery['where'] != '') {
$sql .= ' WHERE ' . static::$dbQuery['where'];
}
if (static::$querySelect['groupBy'] != '') {
$sql .= ' GROUP BY ' . static::$querySelect['groupBy'];
if (static::$dbQuery['groupBy'] != '') {
$sql .= ' GROUP BY ' . static::$dbQuery['groupBy'];
}
if (static::$querySelect['orderBy'] != '') {
$sql .= ' ORDER BY ' . static::$querySelect['orderBy'];
if (static::$dbQuery['orderBy'] != '') {
$sql .= ' ORDER BY ' . static::$dbQuery['orderBy'];
}
if (static::$querySelect['limit'] != '') {
$sql .= ' LIMIT ' . static::$querySelect['limit'];
if (static::$dbQuery['limit'] != '') {
$sql .= ' LIMIT ' . static::$dbQuery['limit'];
}
return $sql;
@@ -267,8 +268,8 @@ class Model
*/
public static function bind(string $value): string
{
$index = ':v_' . count(static::$queryVars);
static::$queryVars[$index] = $value;
$index = ':v_' . count(static::$dbQueryVariables);
static::$dbQueryVariables[$index] = $value;
return $index;
}
@@ -323,13 +324,13 @@ class Model
$result = [];
foreach ($properties as $property) {
if (!in_array($property->name, static::$ignoreSave)) {
if (!in_array($property->name, static::$dbIgnoreSave)) {
$result[$this->camelCaseToSnakeCase($property->name)] = isset($this->{$property->name})
? $this->{$property->name} : null;
}
}
foreach (static::$forceSave as $value) {
foreach (static::$dbForceSave as $value) {
$result[$value] = isset($this->$value)
? $this->$value : null;
}
@@ -370,8 +371,8 @@ class Model
*/
protected static function table(): string
{
if (isset(static::$table)) {
return static::$table;
if (isset(static::$dbTableName)) {
return static::$dbTableName;
}
return static::camelCaseToSnakeCase(static::className()) . 's';
@@ -419,21 +420,21 @@ class Model
foreach ($atts as $key => $value) {
if (isset($value)) {
if (in_array($key, $this->toNull)) {
if (in_array($key, $this->dbSetToNull)) {
$set[] = "$key=NULL";
} else {
$set[] = "$key=:$key";
static::$queryVars[':' . $key] = $value;
static::$dbQueryVariables[':' . $key] = $value;
}
} else {
if (in_array($key, $this->toNull)) {
if (in_array($key, $this->dbSetToNull)) {
$set[] = "$key=NULL";
}
}
}
$table = static::table();
$pk = static::$primaryKey;
$pk = static::$dbPrimaryKey;
$pkv = $this->$pk;
$sql = "UPDATE $table SET " . join(', ', $set) . " WHERE $pk='$pkv'";
static::query($sql);
@@ -455,7 +456,7 @@ class Model
if (isset($value)) {
$into[] = "$key";
$values[] = ":$key";
static::$queryVars[":$key"] = $value;
static::$dbQueryVariables[":$key"] = $value;
}
}
@@ -463,7 +464,7 @@ class Model
$sql = "INSERT INTO $table (" . join(', ', $into) . ") VALUES (" . join(', ', $values) . ")";
static::query($sql);
$pk = static::$primaryKey;
$pk = static::$dbPrimaryKey;
$this->$pk = $db->lastInsertId();
}
@@ -474,7 +475,7 @@ class Model
*/
public function save(): void
{
$pk = static::$primaryKey;
$pk = static::$dbPrimaryKey;
if (isset($this->$pk)) {
$this->update();
} else {
@@ -489,10 +490,10 @@ class Model
public function delete(): void
{
$table = static::table();
$pk = static::$primaryKey;
$pk = static::$dbPrimaryKey;
$sql = "DELETE FROM $table WHERE $pk=:$pk";
static::$queryVars[":$pk"] = $this->$pk;
static::$dbQueryVariables[":$pk"] = $this->$pk;
static::query($sql);
}
@@ -506,7 +507,7 @@ class Model
*/
public static function select(string ...$columns): static
{
static::$querySelect['select'] = $columns;
static::$dbQuery['select'] = $columns;
return new static();
}
@@ -521,7 +522,7 @@ class Model
*/
public static function from(...$tables): static
{
static::$querySelect['from'] = join(', ', $tables);
static::$dbQuery['from'] = join(', ', $tables);
return new static();
}
@@ -591,10 +592,10 @@ class Model
$value = static::bind($value);
}
if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = "$column $operatorOrValue $value";
if (static::$dbQuery['where'] == '') {
static::$dbQuery['where'] = "$column $operatorOrValue $value";
} else {
static::$querySelect['where'] .= " AND $column $operatorOrValue $value";
static::$dbQuery['where'] .= " AND $column $operatorOrValue $value";
}
return new static();
@@ -633,10 +634,10 @@ class Model
$value = static::bind($value);
}
if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = "$column $operatorOrValue $value";
if (static::$dbQuery['where'] == '') {
static::$dbQuery['where'] = "$column $operatorOrValue $value";
} else {
static::$querySelect['where'] .= " OR $column $operatorOrValue $value";
static::$dbQuery['where'] .= " OR $column $operatorOrValue $value";
}
return new static();
@@ -663,10 +664,10 @@ class Model
}
$where = "$column IN (" . join(', ', $arrIn) . ")";
if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = $where;
if (static::$dbQuery['where'] == '') {
static::$dbQuery['where'] = $where;
} else {
static::$querySelect['where'] .= " AND $where";
static::$dbQuery['where'] .= " AND $where";
}
return new static();
@@ -693,10 +694,10 @@ class Model
}
$where = "$column NOT IN (" . join(', ', $arrIn) . ")";
if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = $where;
if (static::$dbQuery['where'] == '') {
static::$dbQuery['where'] = $where;
} else {
static::$querySelect['where'] .= " AND $where";
static::$dbQuery['where'] .= " AND $where";
}
return new static();
@@ -787,7 +788,7 @@ class Model
$operatorOrColumnB = '=';
}
static::$querySelect['leftJoin'] .= ' LEFT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
static::$dbQuery['leftJoin'] .= ' LEFT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
return new static();
}
@@ -822,13 +823,13 @@ class Model
}
if (static::db()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite') {
$currentTable = empty(static::$querySelect['from']) ?
static::table() : static::$querySelect['from'];
static::$querySelect['from'] = $table;
$currentTable = empty(static::$dbQuery['from']) ?
static::table() : static::$dbQuery['from'];
static::$dbQuery['from'] = $table;
return static::leftJoin($currentTable, $columnB, $operatorOrColumnB, $columnA);
}
static::$querySelect['rightJoin'] .= ' RIGHT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
static::$dbQuery['rightJoin'] .= ' RIGHT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
return new static();
}
@@ -862,7 +863,7 @@ class Model
$operatorOrColumnB = '=';
}
static::$querySelect['innerJoin'] .= ' INNER JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
static::$dbQuery['innerJoin'] .= ' INNER JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
return new static();
}
@@ -888,7 +889,7 @@ class Model
public static function crossJoin(
string $table,
): static {
static::$querySelect['crossJoin'] .= ' CROSS JOIN ' . $table;
static::$dbQuery['crossJoin'] .= ' CROSS JOIN ' . $table;
return new static();
}
@@ -902,7 +903,7 @@ class Model
*/
public static function groupBy(string ...$columns): static
{
static::$querySelect['groupBy'] = join(', ', $columns);
static::$dbQuery['groupBy'] = join(', ', $columns);
return new static();
}
@@ -920,9 +921,9 @@ class Model
public static function limit(int $offsetOrQuantity, ?int $quantity = null): static
{
if (is_null($quantity)) {
static::$querySelect['limit'] = $offsetOrQuantity;
static::$dbQuery['limit'] = $offsetOrQuantity;
} else {
static::$querySelect['limit'] = $quantity . ' OFFSET ' . $offsetOrQuantity;
static::$dbQuery['limit'] = $quantity . ' OFFSET ' . $offsetOrQuantity;
}
return new static();
@@ -943,7 +944,7 @@ class Model
public static function orderBy(string $value, string $order = 'ASC'): static
{
if ($value == "RAND") {
static::$querySelect['orderBy'] = 'RAND()';
static::$dbQuery['orderBy'] = 'RAND()';
return new static();
}
@@ -951,7 +952,7 @@ class Model
$order = 'ASC';
}
static::$querySelect['orderBy'] = $value . ' ' . $order;
static::$dbQuery['orderBy'] = $value . ' ' . $order;
return new static();
}
@@ -972,25 +973,25 @@ class Model
{
if (!$resetQuery) {
$backup = [
'select' => static::$querySelect['select'],
'limit' => static::$querySelect['limit'],
'orderBy' => static::$querySelect['orderBy'],
'select' => static::$dbQuery['select'],
'limit' => static::$dbQuery['limit'],
'orderBy' => static::$dbQuery['orderBy'],
];
}
if ($useLimit && static::$querySelect['limit'] != '') {
static::$querySelect['select'] = ['1'];
static::$querySelect['orderBy'] = '';
if ($useLimit && static::$dbQuery['limit'] != '') {
static::$dbQuery['select'] = ['1'];
static::$dbQuery['orderBy'] = '';
$sql = 'SELECT COUNT(1) AS quantity FROM (' . static::buildQuery() . ') AS counted';
$queryResult = static::query($sql, $resetQuery);
$result = $queryResult[0]['quantity'];
} else {
static::$querySelect['select'] = [
"COUNT(" . static::table() . "." . static::$primaryKey . ") as quantity",
static::$dbQuery['select'] = [
"COUNT(" . static::table() . "." . static::$dbPrimaryKey . ") as quantity",
];
static::$querySelect['limit'] = '1';
static::$querySelect['orderBy'] = '';
static::$dbQuery['limit'] = '1';
static::$dbQuery['orderBy'] = '';
$sql = static::buildQuery();
$queryResult = static::query($sql, $resetQuery);
@@ -998,9 +999,9 @@ class Model
}
if (!$resetQuery) {
static::$querySelect['select'] = $backup['select'];
static::$querySelect['limit'] = $backup['limit'];
static::$querySelect['orderBy'] = $backup['orderBy'];
static::$dbQuery['select'] = $backup['select'];
static::$dbQuery['limit'] = $backup['limit'];
static::$dbQuery['orderBy'] = $backup['orderBy'];
}
return (int)$result;
@@ -1016,7 +1017,7 @@ class Model
*/
public static function getById(mixed $id): ?static
{
return static::where(static::$primaryKey, $id)->getFirst();
return static::where(static::$dbPrimaryKey, $id)->getFirst();
}
/**
@@ -1050,10 +1051,10 @@ class Model
}
}
if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = join(' OR ', $where);
if (static::$dbQuery['where'] == '') {
static::$dbQuery['where'] = join(' OR ', $where);
} else {
static::$querySelect['where'] = static::$querySelect['where'] . ' AND (' . join(' OR ', $where) . ')';
static::$dbQuery['where'] = static::$dbQuery['where'] . ' AND (' . join(' OR ', $where) . ')';
}
return new static();
@@ -1130,8 +1131,8 @@ class Model
public function setNull(string ...$attributes): void
{
foreach ($attributes as $att) {
if (!in_array($att, $this->toNull)) {
$this->toNull[] = $att;
if (!in_array($att, $this->dbSetToNull)) {
$this->dbSetToNull[] = $att;
}
}
}