List Manager Forum

Calculated PHP to insert value from other table

User aliwari 2024-03-04 04:00:40

Hi,

I created a table named 'nabo_nblst_contacts' with a 'acym_user_id' field in the list manager. The 'acym_user_id' field is supposed to have calculated PHP to retrieve the value of the 'id' field from the 'nabo_acym_user' table. However, I encountered an issue where no value is being inserted into the 'acym_user_id' field, and I received a PHP Warning stating:
'Undefined array key "table_name" in C:\inetpub\nblst\administrator\components\com_listmanager\Model\ListheaderModel.php on line 193

Here is the code I'm using "<?php
// Load the AcyMailing library
$postData = $_REQUEST;

$ds = DIRECTORY_SEPARATOR;
$helperPath = rtrim(JPATH_ADMINISTRATOR, $ds) . $ds . 'components' . $ds . 'com_acym' . $ds . 'helpers' . $ds . 'helper.php';

// Check if the helper file exists before including it
if (file_exists($helperPath)) {
include_once $helperPath;

// Check if the userClass is instantiated successfully
if (class_exists('AcyMailing\Classes\UserClass')) {
try {
// Instantiate the UserClass
$userClass = new AcyMailing\Classes\UserClass();

// Connect to your Joomla database
$db = JFactory::getDbo();

// Fetch the 'id' value from the 'nabo_acym_user' table
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__nabo_acym_user'));

$db->setQuery($query);
$userId = $db->loadResult();

// Check if 'acym_user_id' exists in 'nabo_nblst_contacts'
$query = $db->getQuery(true)
->select($db->quoteName('acym_user_id'))
->from($db->quoteName('#__nabo_nblst_contacts'))
->where($db->quoteName('acym_user_id') . ' = ' . $db->quote($userId));

$db->setQuery($query);
$existingUserId = $db->loadResult();

// If 'acym_user_id' doesn't exist, insert it
if (!$existingUserId) {
$query = $db->getQuery(true)
->insert($db->quoteName('#__nabo_nblst_contacts'))
->columns($db->quoteName(array('acym_user_id')))
->values($db->quote($userId));

$db->setQuery($query);
$db->execute();
}

} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
}
}
?>

Could you please advise me on what I might be missing in the code?




Regards

Moonsoft support 2024-03-04 09:37:20

Hi,
sorry but we don't get the general point of your code (for ex, we can't see what are you instanciating the acymailing userclass for, and it seems you're trying to get a user id using a query without any condition (so you will always get the first user id stored)?. Anyway, this is not the way the calculated php section is intended to work, you are not supposed to insert values into any table using this code. The calculated php allows you to perform calculations based on the stored field, or any other from the same record, so you can change dynamically the output shown at the table, but you can't change the real value stored at the database, this code is executed when the component recovers records to be printed at the page.

An example, let's say you have stored your Joomla user id (using the listmanager type user id) , you can use it to launch a query to other table and show a different output at that cell.

Hope this helps, regards

User aliwari 2024-03-05 13:56:10

Hi,

I appreciate your clarification.

...
Support/development

Perfect for small code changes or to correct any bug at your site

Buy now!
...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!