Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JFactory::getDbo() deprecated #601

Closed
dimoss opened this issue Dec 25, 2022 · 8 comments
Closed

JFactory::getDbo() deprecated #601

dimoss opened this issue Dec 25, 2022 · 8 comments

Comments

@dimoss
Copy link

dimoss commented Dec 25, 2022

Before reporting an issue please check that it has not already been fixed by reviewing the changelog in the Wiki or other open issues that may be pending confirmation. Thank you.

To Reproduce
Changing depreacted code in calc element based on upgrading instruction to replace Factory::getDbo() to:
$db = version_compare(\Joomla\CMS\Version::MAJOR_VERSION, "4", ">=") ? Factory::getContainer()->get('DatabaseDriver') : Factory::getDbo();

Expected behavior
Removing notice:
Joomla\CMS\Factory::getDbo() is deprecated. Load the database from the dependency injection container.

Actual behavior
Error:
Class "JoomlaCMSVersion" not found

Screenshots

Additional context
Using J!4.2.5 with F4.0beta4b in a clean J!4+F4 test site.

@trob
Copy link
Collaborator

trob commented Dec 25, 2022

I didn't test.
But I think

  1. if you know you are on J!4 you don't need the version compare at all
  2. in calc elements etc you need to escape \ (so use \\ in these cases)

🎄🎄🎄

@dimoss
Copy link
Author

dimoss commented Dec 25, 2022

I didn't test. But I think

  1. if you know you are on J!4 you don't need the version compare at all
  2. in calc elements etc you need to escape \ (so use \\ in these cases)

🎄🎄🎄

Thanks @trob

Found a solution based on this https://github.com/joomla/joomla-cms/discussions/38111 escaping \ as you suggested. So the final one for calc element is:

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
$db = Factory::getContainer()->get(DatabaseInterface::class);

On a separate note I used the same in j4upgrade.php file provided by @skurvish and works fine. Thanks @skurvish for the file.

Merry Christmas to everyone!

@dimoss dimoss closed this as completed Dec 25, 2022
@wezetel
Copy link

wezetel commented Dec 25, 2022

I have to admit: I'm too stupid to implement this solution, even this is not the first try to avoid the depreciated warning. I just can get my application working with the old solution.
In the following picture you see what is working in a calc element:
grafik
If I comment out line 7 and and enable lines 3 to 5 my application hangs.
Probably I'm to blind to see what is wrong with the code, so any hint is welcome

@trob
Copy link
Collaborator

trob commented Dec 25, 2022

Did you try
Joomla\\CMS\\Factory::getContainer()->get('DatabaseDriver');

@wezetel
Copy link

wezetel commented Dec 26, 2022

@trob Just gave it a try. It works, but only if I disable Ajax.
And that's not really what I need, because the calc field should display a hint to the user depending on the selection of another field. And without Ajax the hint is just stored on save and not displayed.

@trob
Copy link
Collaborator

trob commented Dec 26, 2022

Just tested:

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
$myDb = Factory::getContainer()->get('DatabaseDriver');
//also working with $myDB = Factory::getContainer()->get(DatabaseInterface::class);
$myQuery = $myDb->getQuery(true);

$myQuery
    ->select(array('feld'))
    ->from('test17')
    ->where('id = ' . $myDb->quote(4));

$myDb->setQuery($myQuery);

$x=$myDb->loadResult();
$y=': {atest2___feld3}';
return $x.$y;

is working with Ajax calc, with and without Ajaxfied links.
It also doesn't matter if you use single or double \ (in the DB it's stored with \\ resp. \\\\ anyway because of ???)

If you get a sticky spinner with Ajax calc check your browser's network response to see the error.

You should have "use ..." at the beginning of our code (although I think it doesn't matter).
$anliegenid may be 0 in new records or if not selected?
You have to handle this case to get a result in $fieldA.

@wezetel
Copy link

wezetel commented Dec 27, 2022

Thanks for your efforts, @trob
At the moment, I don't have success, but as it is working for you and others, I will revisit the problem later again with a little more distance in my head....

@trob
Copy link
Collaborator

trob commented Feb 13, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants