Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
Clubphp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Clubphp
Commits
c857f2e4
Commit
c857f2e4
authored
Apr 08, 2026
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update 1 files via Son of Anton
parent
fb8477ef
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
35 deletions
+6
-35
Database.php
app/Core/Database.php
+6
-35
No files found.
app/Core/Database.php
View file @
c857f2e4
...
...
@@ -12,15 +12,8 @@ class Database
private
array
$afterDeleteHooks
=
[];
private
array
$tableCache
=
[];
public
function
__construct
(
array
$config
)
public
function
__construct
(
string
$host
=
'127.0.0.1'
,
int
$port
=
3306
,
string
$dbName
=
''
,
string
$user
=
'root'
,
string
$pass
=
''
,
string
$charset
=
'utf8mb4'
)
{
$host
=
$config
[
'host'
]
??
'127.0.0.1'
;
$port
=
$config
[
'port'
]
??
'3306'
;
$dbName
=
$config
[
'name'
]
??
''
;
$user
=
$config
[
'user'
]
??
'root'
;
$pass
=
$config
[
'pass'
]
??
''
;
$charset
=
$config
[
'charset'
]
??
'utf8mb4'
;
$dsn
=
"mysql:host=
{
$host
}
;port=
{
$port
}
;dbname=
{
$dbName
}
;charset=
{
$charset
}
"
;
$this
->
pdo
=
new
\PDO
(
$dsn
,
$user
,
$pass
,
[
...
...
@@ -47,6 +40,11 @@ class Database
return
$stmt
;
}
public
function
raw
(
string
$sql
)
:
void
{
$this
->
pdo
->
exec
(
$sql
);
}
public
function
select
(
string
$sql
,
array
$params
=
[])
:
array
{
return
$this
->
query
(
$sql
,
$params
)
->
fetchAll
();
...
...
@@ -101,13 +99,11 @@ class Database
if
(
!
empty
(
$this
->
afterUpdateHooks
))
{
try
{
// Try to extract entity ID from WHERE clause
$entityId
=
$this
->
extractIdFromWhere
(
$where
,
$whereParams
);
if
(
$entityId
!==
null
)
{
$oldRecord
=
$this
->
selectOne
(
"SELECT * FROM `
{
$table
}
` WHERE `id` = ?"
,
[
$entityId
]);
}
else
{
// Fallback: fetch first matching record
$oldRecord
=
$this
->
selectOne
(
"SELECT * FROM `
{
$table
}
` WHERE
{
$where
}
LIMIT 1"
,
$whereParams
);
if
(
$oldRecord
&&
isset
(
$oldRecord
[
'id'
]))
{
$entityId
=
(
int
)
$oldRecord
[
'id'
];
...
...
@@ -145,7 +141,6 @@ class Database
if
(
$entityId
!==
null
)
{
$newRecord
=
$this
->
selectOne
(
"SELECT * FROM `
{
$table
}
` WHERE `id` = ?"
,
[
$entityId
]);
}
elseif
(
$oldRecord
)
{
// Construct approximate new record from old + changes
$newRecord
=
array_merge
(
$oldRecord
,
$data
);
}
}
catch
(
\Throwable
$e
)
{
...
...
@@ -169,7 +164,6 @@ class Database
public
function
delete
(
string
$table
,
string
$where
,
array
$whereParams
=
[])
:
int
{
// Fetch record before deleting
$oldRecord
=
null
;
$entityId
=
null
;
...
...
@@ -254,37 +248,21 @@ class Database
// HOOK REGISTRATION
// ═══════════════════════════════════════════
/**
* Hook: called after a successful INSERT.
* Callback: function(string $table, array $data, ?int $insertId)
*/
public
function
onAfterInsert
(
callable
$callback
)
:
void
{
$this
->
afterInsertHooks
[]
=
$callback
;
}
/**
* Hook: called before an UPDATE executes.
* Callback: function(string $table, array $newData, ?int $entityId, ?array $oldRecord)
*/
public
function
onBeforeUpdate
(
callable
$callback
)
:
void
{
$this
->
beforeUpdateHooks
[]
=
$callback
;
}
/**
* Hook: called after a successful UPDATE.
* Callback: function(string $table, array $changedData, ?int $entityId, ?array $oldRecord, ?array $newRecord)
*/
public
function
onAfterUpdate
(
callable
$callback
)
:
void
{
$this
->
afterUpdateHooks
[]
=
$callback
;
}
/**
* Hook: called after a successful DELETE.
* Callback: function(string $table, array $oldRecord, ?int $entityId)
*/
public
function
onAfterDelete
(
callable
$callback
)
:
void
{
$this
->
afterDeleteHooks
[]
=
$callback
;
...
...
@@ -294,12 +272,8 @@ class Database
// INTERNAL HELPERS
// ═══════════════════════════════════════════
/**
* Try to extract the entity ID from a WHERE clause like `id` = ? or id = ?
*/
private
function
extractIdFromWhere
(
string
$where
,
array
$params
)
:
?
int
{
// Match patterns: `id` = ?, id = ?, `id`=?
if
(
preg_match
(
'/^`?id`?\s*=\s*\?/'
,
trim
(
$where
))
&&
!
empty
(
$params
))
{
$val
=
$params
[
0
];
if
(
is_numeric
(
$val
))
{
...
...
@@ -309,9 +283,6 @@ class Database
return
null
;
}
/**
* Get last insert ID
*/
public
function
lastInsertId
()
:
int
{
return
(
int
)
$this
->
pdo
->
lastInsertId
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment