Documentation

UserScopedQuery
in package

Helper class for adding user scope filtering to raw SQL queries.

Use this class when you need to execute raw SQL but still want automatic user scope filtering. For most cases, prefer using QueryBuilder which handles this automatically.

Usage:

// Get user scope condition for WHERE clause (for raw SQL)
$sql = "SELECT * FROM words WHERE WoLgID = 1"
     . UserScopedQuery::forTable('words');

// For prepared statements
$bindings = [1]; // WoLgID = 1
$sql = "SELECT * FROM words WHERE WoLgID = ?"
     . UserScopedQuery::forTablePrepared('words', $bindings);

// Get user_id for INSERT (when using raw SQL)
$userId = UserScopedQuery::getUserIdForInsert('words');
if ($userId !== null) {
    $data['WoUsID'] = $userId;
}
Tags
category

Database

since
3.0.0

Table of Contents

Constants

USER_SCOPED_TABLES  = ['languages' => 'LgUsID', 'texts' => 'TxUsID', 'words' => 'WoUsID', 'tags' => 'TgUsID', 'text_tags' => 'T2UsID', 'news_feeds' => 'NfUsID', 'settings' => 'StUsID', 'local_dictionaries' => 'LdUsID']
Mapping of user-scoped tables to their user ID column.

Methods

forTable()  : string
Get WHERE condition for user scope filtering.
forTablePrepared()  : string
Get WHERE condition for user scope filtering (prepared statement version).
getUserIdColumn()  : string|null
Get the user ID column name for a table.
getUserIdForInsert()  : int|null
Get the user ID value to use for inserts.
getUserScopedTables()  : array<string, string>
Get the list of all user-scoped tables.
insertColumn()  : string
Get INSERT column fragment for user scope.
insertValue()  : string
Get INSERT value fragment for user scope (raw SQL version).
insertValuePrepared()  : string
Get INSERT value fragment for user scope (prepared statement version).
isUserScopedTable()  : bool
Check if a table is user-scoped.
whereClause()  : string
Get a standalone WHERE clause for user scope.
whereClausePrepared()  : string
Get a standalone WHERE clause (prepared statement version).

Constants

USER_SCOPED_TABLES

Mapping of user-scoped tables to their user ID column.

private array<string, string> USER_SCOPED_TABLES = ['languages' => 'LgUsID', 'texts' => 'TxUsID', 'words' => 'WoUsID', 'tags' => 'TgUsID', 'text_tags' => 'T2UsID', 'news_feeds' => 'NfUsID', 'settings' => 'StUsID', 'local_dictionaries' => 'LdUsID']

Methods

forTable()

Get WHERE condition for user scope filtering.

public static forTable(string $tableName[, string $alias = '' ][, string $_parentTable = '' ]) : string

Returns a SQL fragment like " AND WoUsID = 1" when user scope should be applied, or empty string otherwise.

Parameters
$tableName : string

The table name (without prefix)

$alias : string = ''

Optional table alias to prefix the column

$_parentTable : string = ''

Optional parent table for inherited scope (unused, kept for API compat)

Return values
string

SQL WHERE condition fragment (includes leading AND)

forTablePrepared()

Get WHERE condition for user scope filtering (prepared statement version).

public static forTablePrepared(string $tableName, array<int, mixed> &$bindings[, string $alias = '' ][, string $_parentTable = '' ]) : string

Returns a SQL fragment like " AND WoUsID = ?" and adds the user ID to the provided bindings array.

Parameters
$tableName : string

The table name (without prefix)

$bindings : array<int, mixed>

Reference to bindings array

$alias : string = ''

Optional table alias

$_parentTable : string = ''

Optional parent table for inherited scope (unused, kept for API compat)

Return values
string

SQL WHERE condition fragment (includes leading AND)

getUserIdColumn()

Get the user ID column name for a table.

public static getUserIdColumn(string $tableName) : string|null
Parameters
$tableName : string

The table name (without prefix)

Return values
string|null

The column name or null if not user-scoped

getUserIdForInsert()

Get the user ID value to use for inserts.

public static getUserIdForInsert(string $tableName) : int|null

Returns the current user ID when multi-user mode is enabled and a user is authenticated. Returns null otherwise.

Parameters
$tableName : string

The table name (without prefix)

Return values
int|null

The user ID or null

getUserScopedTables()

Get the list of all user-scoped tables.

public static getUserScopedTables() : array<string, string>
Return values
array<string, string>

Table name => user ID column mapping

insertColumn()

Get INSERT column fragment for user scope.

public static insertColumn(string $tableName) : string

Returns a SQL fragment like ", WoUsID" to append to INSERT column list when user scope should be applied, or empty string otherwise.

Usage:

$sql = "INSERT INTO words (WoLgID, WoText" . UserScopedQuery::insertColumn('words') .
        ") VALUES (?, ?" . UserScopedQuery::insertValuePrepared('words', $bindings) . ")";
Parameters
$tableName : string

The table name (without prefix)

Return values
string

SQL column fragment (includes leading comma) or empty string

insertValue()

Get INSERT value fragment for user scope (raw SQL version).

public static insertValue(string $tableName) : string

Returns a SQL fragment like ", 1" with the actual user ID value when user scope should be applied, or empty string otherwise.

Parameters
$tableName : string

The table name (without prefix)

Return values
string

SQL value fragment (includes leading comma) or empty string

insertValuePrepared()

Get INSERT value fragment for user scope (prepared statement version).

public static insertValuePrepared(string $tableName, array<int, mixed> &$bindings) : string

Returns a SQL fragment like ", ?" and adds the user ID to bindings when user scope should be applied, or empty string otherwise.

Parameters
$tableName : string

The table name (without prefix)

$bindings : array<int, mixed>

Reference to bindings array

Return values
string

SQL value fragment (includes leading comma) or empty string

isUserScopedTable()

Check if a table is user-scoped.

public static isUserScopedTable(string $tableName) : bool
Parameters
$tableName : string

The table name (without prefix)

Return values
bool

True if the table requires user_id filtering

whereClause()

Get a standalone WHERE clause for user scope.

public static whereClause(string $tableName[, string $alias = '' ]) : string

Returns "WHERE WoUsID = 1" when applicable, empty string otherwise. Use this when you need a WHERE clause that only contains user scope.

Parameters
$tableName : string

The table name (without prefix)

$alias : string = ''

Optional table alias

Return values
string

SQL WHERE clause or empty string

whereClausePrepared()

Get a standalone WHERE clause (prepared statement version).

public static whereClausePrepared(string $tableName, array<int, mixed> &$bindings[, string $alias = '' ]) : string
Parameters
$tableName : string

The table name (without prefix)

$bindings : array<int, mixed>

Reference to bindings array

$alias : string = ''

Optional table alias

Return values
string

SQL WHERE clause or empty string


        
On this page

Search results