Joomla\CMS\Factory in J3

Hello,
I am working on updating my custom PHP code in Fabrik to work with J4/F4. All I really need to change are my calls to get the database connection. I'm following the instructions here: https://github.com/trobfab/fabrik/wiki/Upgrading-from-fabrik3.10-to-fabrik4

The site is currently on J3.10.11, Fabrik 3.10, PHP 7.4.33. I'm testing out the change in a Display element.

I've replaced
$mydb = JFactory::getDBO();

with
$mydb = version_compare(\Joomla\CMS\Version::MAJOR_VERSION, "4", ">=") ? Factory::getContainer()->get('DatabaseDriver') : Factory::getDbo();

When I view the list, I get this error
0 - Class 'JoomlaCMSVersion' not found

As a test, I put this code in the display element and removed all other code, just to see if I could call something using \Joomla\CMS\Factory.

$user = \Joomla\CMS\Factory::getUser();
return $user;

And I get this error:
0 - Class 'JoomlaCMSFactory' not found

Is there something specific I need to do to use this in an element's PHP code? I wouldn't think so, but I thought I'd double check.
 
You are posting in the J3/F3 Forum.
If you are really on J3/F3 you can't use J4/F4 calls...
Just do a F3 Github-Update..
 
The site is J3, F3, PHP 7.4.33

The way I read the instructions (included below) if I'm using Factory::getDbo() I could change it to version_compare(\Joomla\CMS\Version::MAJOR_VERSION, "4", ">=") ? Factory::getContainer()->get('DatabaseDriver') : Factory::getDbo() so it checks for the Joomla version and uses the appropriate class based on that check. And, this can be done before upgrading anything as long as it's on J3.10+

I'm using JFactory::getDBO() not Factory::getDbo(), but I figured it would still apply.

Did I misunderstand something?

---------------------------------------------

From here https://github.com/trobfab/fabrik/wiki/Upgrading-from-fabrik3.10-to-fabrik4

Things that users should check before they do their upgrade.
Review the list of Backward Compatibility Issues . As long as you are J3.10+ you must make these adjustments prior to upgrading to J4.

All hard coded php code (default eval, calc elements, allow date functions, eval options, eval populate etc) needs to have proper namespace use statements such as
use Joomla\CMS\Factory; or calling it directly in you code such as
$user = \Joomla\CMS\Factory::getUser();
There is no guarantee any of the J prefixed classnames will work. Placing use statements in eval'ed code has been tested on php 8.1.

In order to help you identify all of your forms/lists/elements etc that have php code attached to them you may find this file to be useful. It will print a nice report for you. I find the small component called jumi very useful for these types of things. The original developer of Jumi is long gone but here is a version that runs on J3 & J4.

Also, if you are using Factory::getDbo() in your custom php code we suggest you change that now to the following. It will eliminate a bunch of deprecated notices when you start testing your code on J4.

$db = version_compare(\Joomla\CMS\Version::MAJOR_VERSION, "4", ">=") ? Factory::getContainer()->get('DatabaseDriver') : Factory::getDbo();
If you are already on J!4 you don't need the version check, just
use Joomla\CMS\Factory; $db = Factory::getContainer()->get('DatabaseDriver');
or
$db = Joomla\CMS\Factory::getContainer()->get('DatabaseDriver');

The bootstrap classes have changed for BS5, however, we have added code to transpose your existing classes, either input-xx, spanx or col-md, to the new classes on the fly so you do not need to change them prior to upgrading. Also, whenever you edit an element that has a bootstrap class associated with it, it will be updated on save to the new class.get('DatabaseDriver');
 
I assume in J3 F3 you have to use \\
But I also would suggest to update to J4 F4 and test then.
In this case you dont need the version test at all.

Gesendet von meinem SM-G930F mit Tapatalk
 
Last edited:
I've never liked this version check.
Usually I upgrade the system (php), then to Joomla4 and afterwards test Joomla extensions (F4)
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top