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