Breaking Changes in v1.10.00: Difference between revisions
Miqrogroove (talk | contribs) (→Queries: tablepre) |
Miqrogroove (talk | contribs) (→Queries: Example) |
||
Line 35: | Line 35: | ||
=== The tablepre Variable === | === The tablepre Variable === | ||
Instead of providing a global constant named <code>X_PREFIX</code>, XMB now stores the <code>$tablepre</code> variable from <code>config.php</code> as a property of the shared <code>vars</code> service. The direct replacement | Instead of providing a global constant named <code>X_PREFIX</code>, XMB now stores the <code>$tablepre</code> variable from <code>config.php</code> as a property of the shared <code>vars</code> service. The direct replacement looks like this: | ||
<pre> | <pre> | ||
Line 41: | Line 41: | ||
$statement = 'SELECT * FROM' . $vars->tablepre . 'ranks'; | $statement = 'SELECT * FROM' . $vars->tablepre . 'ranks'; | ||
</pre> | |||
=== Example === | |||
Here is an actual query from the old <code>cp2.php</code> file: | |||
<pre> | |||
require ROOT . 'header.php'; | |||
$query = $db->query("SELECT * FROM ".X_PREFIX."restricted ORDER BY id"); | |||
</pre> | |||
Here is how it changed in the new <code>restrictions.php</code> file during development of v1.10.00: | |||
<pre> | |||
require ROOT . 'header.php'; | |||
$db = \XMB\Services\db(); | |||
$vars = \XMB\Services\vars(); | |||
$query = $db->query("SELECT * FROM " . $vars->tablepre . "restricted ORDER BY id"); | |||
</pre> | </pre> |
Revision as of 10:59, 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 ROOT . '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");