mirror of
https://github.com/avecms/AVE.cms.git
synced 2026-08-01 08:45:44 +00:00
2.4 KiB
2.4 KiB
Table names (resolvers)
Prefixes differ for different data domains (content, admin system tables, module tables, public shell), so table name is always obtained via resolver, rather than writing it as a line. This gives the correct prefix and protection against typos.
Resolvers
| Class | Domain | Example result |
|---|---|---|
App\Content\ContentTables::table($s) |
Content: documents, headings, fields | ave_documents |
App\Common\SystemTables::table($s) |
Administrator system tables (by whitelist) | ave_admin_notes |
App\Content\ExtensionTables::table($s) |
Core Content Tables | ave_todos |
App\Content\PublicShellTables::table($s) |
Public shell (sessions, etc.) | ave_public_sessions |
use App\Content\ContentTables;
$t = ContentTables::table('documents');
$rows = DB::query('SELECT * FROM ' . $t . ' WHERE Id = %i', 42)->getAssoc();
Suffix validation
ContentTables::table()checks the suffix with the regular^[a-z0-9_]+$and throws exception for invalid name.SystemTables::table()additionally checks the suffix with whitelist - new The system table must first be added to the allowed list.
ContentTables::table('documents'); // ok
ContentTables::table('users; DROP …'); // InvalidArgumentException
Why not hardcode
// ПЛОХО — префикс зашит, сломается в другом окружении/домене данных
$rows = DB::query('SELECT * FROM ' . ContentTables::table('documents'))->getAll();
// ХОРОШО — префикс подставит резолвер
$t = ContentTables::table('documents');
$rows = DB::query('SELECT * FROM ' . $t)->getAll();
Frequent trick: table constant in the model
class Model
{
public static function documentsTable() { return ContentTables::table('documents'); }
public static function rubricsTable() { return ContentTables::table('rubrics'); }
public static function one($id)
{
return DB::query(
'SELECT d.*, r.rubric_title FROM ' . self::documentsTable() . ' d'
. ' LEFT JOIN ' . self::rubricsTable() . ' r ON r.Id = d.rubric_id'
. ' WHERE d.Id = %i',
(int) $id
)->getAssoc();
}
}
The list of available content tables is in the schema sources; system - in whitelist inside
SystemTables.