Issue with code field

1 month 3 weeks ago #332655 by activha
Issue with code field was created by activha

I have an issue with code field which does not allow the use of "use"
How could I call the DB from within CB ?

use Joomla\CMS\Factory;
$db    = Factory::getDbo();
$query = "SELECT parent_id FROM #__affiliate_tracker_accounts where user_id='[user_id]' ";
$comid= $db->loadResult();

$query = "SELECT a.firstname, a.lastname, a.user_id FROM #__comprofiler a inner join #__affiliate_tracker_accounts b on where'$comid' ";
$comProfiler_user= $db->loadObject();

$nom = $comProfiler_user->lastname;
$prenom = $comProfiler_user->firstname;
$userid = $comProfiler_user->user_id;

echo '<a href="profil/'.$userid.'" >'.$prenom.' '.$nom.'</a>';

Please Log in to join the conversation.

1 month 3 weeks ago #332656 by krileon
Replied by krileon on topic Issue with code field
That's not caused by CB Code Field. That's just how PHP works. use statements MUST be at the top of the PHP file being executed. You can't put them inline wherever you like. So your options are to include a PHP file and run your code from there or to supply the full path. Example as follows.

FROM: Factory::getDbo();
TO: \Joomla\CMS\Factory::getDbo();

You shouldn't be nor need to call Joomla API directly. The benefit of CB is it's very platform agnostic. Meaning if you use our API as much as possible you don't really have to worry about what version of Joomla you're using. You can access CBs global database object with the following.
global $_CB_database;

$query	=	"SELECT " . $_CB_database->NameQuote( 'parent_id' )
		.	" FROM " . $_CB_database->NameQuote( '#__affiliate_tracker_accounts' )
		.	" WHERE " . $_CB_database->NameQuote( 'user_id' ) . " = " . $user->getInt( 'id', 0 );
$_CB_database->setQuery( $query );
$comid	=	$_CB_database->loadResult();

$query	=	"SELECT cb." . $_CB_database->NameQuote( 'firstname' )
		.	", cb." . $_CB_database->NameQuote( 'lastname' )
		.	", cb." . $_CB_database->NameQuote( 'user_id' )
		.	" FROM " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS cb"
		.	" INNER JOIN " . $_CB_database->NameQuote( '#__affiliate_tracker_accounts' ) . " AS a"
		.	" ON a." . $_CB_database->NameQuote( 'user_id' ) . " = cb." . $_CB_database->NameQuote( 'id' )
		.	" WHERE a." . $_CB_database->NameQuote( 'id' ) . " = " . (int) comid;
$_CB_database->setQuery( $query );
$comProfiler_user	=	$_CB_database->loadObject();

Note I've modified your query to be safely escaped. Please be careful directly accessing the database if you do not know how to safely escape as you risk creating a vulnerability.

Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

1 month 3 weeks ago #332658 by activha
Replied by activha on topic Issue with code field
Thanks a lot for this :-)

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.189 seconds

Facebook Twitter LinkedIn