Problem in code section of auto action with trigger onAfterCreateComment

6 years 8 months ago #296333 by timstohr
Hi Kyle,

we have a strange problem, when trying to insert a string into our sql data base with results obtained via auto actions. More specifically,
in Joomla in the Auto Action section we defined a new auto action (activity_onAfterCreateComment) with php-code in the action tab that looks like this
//Prepare db
$db = JFactory::getDbo();

//Get asset via comment id
$comment_id = [var1_id];
$db->setQuery("SELECT  `asset` FROM `#__comprofiler_plugin_activity_comments`  WHERE `id` = ".$comment_id );
$db->execute();
$asset = $db->loadResult();

//Insert asset id
$db->setQuery("INSERT INTO `#__testing` (`a`, `b`) VALUES (".$asset.",".$asset .")");
$db->execute();

We are just querying the asset id from the post a user has commented on, which we would like to store into a database.
However, the insert does not work, in the sense that it only saves integers, although the database is configured to save text. Varchar as the database type did not work either.

Another question would be, how to actually get the asset id via the triggering of the auto action, because [var1_asset] or [asset] did not work.

Finally, is there any way to debug auto action code, i.e. show/print the contents of the variables defined in the action tab.

Thanks, Tim

Please Log in to join the conversation.

6 years 8 months ago - 6 years 8 months ago #296352 by krileon
You don't have quotes around $asset inside of the IN statement. The query is fatally failing. For example you're doing the below.

INSERT INTO `#__testing` (`a`, `b`) VALUES (profile.42,profile.42)

That's not fail SQL. You'd need to have the below.

INSERT INTO `#__testing` (`a`, `b`) VALUES ('profile.42','profile.42')

Which is done by properly quoting using APIs as follows.

$db->setQuery("INSERT INTO `#__testing` ( `a`, `b` ) VALUES ( " . $db->quote( $asset ) . ", " . $db->quote( $asset ) . " )");

In regards to that trigger its variables are as follows.

$_PLUGINS->trigger( 'activity_onAfterCreateComment', array( $this ) );

This means the only variable it has is var1, which is the comment object. To get the asset you'd do [var1_asset]. You however always need to treat a substitution as a string. So if you replaced ".$asset." in your code with [var1_asset] it will still fail and needs to be surrounded with quotes. Examples as follows.

Invalid
$asset = [var1_asset];
Valid
$asset = '[var1_asset]';


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.

Moderators: beatnantkrileon
Time to create page: 0.244 seconds

Facebook Twitter LinkedIn