Breaking Changes in v1.10.00: Difference between revisions

From XMBdocs
(→‎Queries: Example)
Line 48: Line 48:


<pre>
<pre>
require ROOT . 'header.php';
require 'header.php';


$query = $db->query("SELECT * FROM ".X_PREFIX."restricted ORDER BY id");
$query = $db->query("SELECT * FROM ".X_PREFIX."restricted ORDER BY id");

Revision as of 11:00, 2 January 2025

Customizing and migrating to v1.10.00 will require some new changes.

Queries

There are three main changes that affect all database queries in XMB.

Global $db Service Locator

Instead of providing a global variable named $db, XMB now offers two services named db and sql.

For a direct replacement of the global shared object reference, just add this near the top of your file:

$db = \XMB\Services\db();

If you need to create a 2nd database connection (to a different account or server) within your script, then use the revised class name instead:

$db2 = new \XMB\MySQLiDatabase(debug: false, logErrors: true);
$db2->connect(...);

Internally, XMB is starting to use methods of the shared sql service instead of direct or prepared statements. This results in cleaner and more secure code. For example, to get the shared service and check your inbox:

$sql = \XMB\Services\sql();
$vars = \XMB\Services\vars();

$msgCount = $sql->countU2UInbox($vars->self['username']);

If you wish to write similar query methods, the best practice is to create your own object class rather than try to modify the XMB service.

The tablepre Variable

Instead of providing a global constant named X_PREFIX, XMB now stores the $tablepre variable from config.php as a property of the shared vars service. The direct replacement looks like this:

$vars = \XMB\Services\vars();

$statement = 'SELECT * FROM' . $vars->tablepre . 'ranks';

Example

Here is an actual query from the old cp2.php file:

require 'header.php';

$query = $db->query("SELECT * FROM ".X_PREFIX."restricted ORDER BY id");

Here is how it changed in the new restrictions.php file during development of v1.10.00:

require ROOT . 'header.php';

$db = \XMB\Services\db();
$vars = \XMB\Services\vars();

$query = $db->query("SELECT * FROM " . $vars->tablepre . "restricted ORDER BY id");