Calc element not storing to DB

Status
Not open for further replies.
ok, i updated from github, nothing changed. i found out why the element was not showing in list view: when i disable calc on save the 'return 1;' part will show in list view properly.
Nevertheless, the initial problem with the code of the opening post still persists. Even this does not seem to work:
Code:
$ddval = JRequest::getVar(timerec___project_title);
return $ddval;

The problem seems to be that it is not stored which value was selected on form saving. Although it is displayed, if i open details view, the calculation of the selected value is not run. This leads to the other field being blank.
 
Your code in that last post won't work, you need to put quotes round the element name.

I've tested this exhaustively here, and I simply cannot replicate your problem. It always saves the calc value to the underlying table regardless of "only calc on save" setting (which is correct behavior), and it always shows the correct value in List display.

You'll have to fill in your Fabrik Sites info, with a backend super admin, and ftp access (or have exTplorer installed in J!), and give me the name of the calc element I need to look at, etc.

-- hugh
 
well i got the code above to work. the problem was that the database value still was "1" from the tests i made before and could not be changed if "only calc on save" was disabled. i cleared all data from the list and now got it to work, with or without "only calc on save".

The original problem seems to be the SQL query.
At the moment, i use this code:
Code:
$ddval = JRequest::getVar('timerec___project_title');
$db = JFactory::getDbo();
 
$query = "SELECT project_number FROM projects WHERE project_title = '$ddval'";
$db->setQuery($query);
$views = $db->loadResult();
 
return $query;
which returns the SQL query. When selecting a value from the Dropdown, it displays correctly: SELECT project_number FROM projects WHERE project_title = 'Projekt 1' for example.
But when saving this changes to SELECT project_number FROM projects WHERE project_title = 'Array' , which shows why there was nothing stored before, because the SQL query was NULL. A least the wrong query is stored to DB and displayed in list view :) Is there a way to save the correct SQL query? Because $ddval is clearly a string and not an array....


You'll have to fill in your Fabrik Sites info, with a backend super admin, and ftp access (or have exTplorer installed in J!), and give me the name of the calc element I need to look at, etc.
Do you still need that? I dont know where to fill that in, you need access to the backend of my site? Should i send you the info via pm?

Thanks for your help :)
 
Depending what element type project_title is, yes, it may well be an array in the raw request data.

Also, remember that JRequest::getVar() is only going to work when submitting a form, as what it does is grab data from the request, i.e. URL (query string) data from $_REQUEST, or posted form form data from $_POST. Which don't exist during list display.

So if you run that code during list display, i.e. "Calc on save only" set to No, it won't work as expected.

The best way would be to use Fabrik's getElementData() ...

PHP:
$ddval = $formModel->getElementData('timerec___project_title');

... which should work in either case, and return either the data being submitted, the data for the row being displayed, or the data for the form being displayed, depending on the context of the call. It should also solve the array vs not an array issue.

If you need the raw data, add a second arg of true:

PHP:
$ddval = $formModel->getElementData('timerec___project_title', true);

-- hugh
 
Note also that as your calc involves a database query, you really should set "Calc on save only" to Yes. You don't want to be running that calc on list display, as it'll add X number of queries to your page load, where X is the number of rows being displayed in the list. So if you display 100 rows, that's a 100 extra database queries for every page load of that list.

That's really what that "Calc on save only" option is for. If your calc is light weight, with no database querying, like (say) concatenating first and last name elements into a full name ("return '{table___firstname}' . ' ' . '{table___lastname}';") then it doesn't really matter if it runs every time you display the calc element. But if it's involving database querying or any other resource intensive code, you really only want to run it when you save the form ... unless it involves data which is outside of that form, i.e. may change independently between form submissions.

-- hugh
 
Thanks for your help.
I tried to use the code you provided, but get a Servererror 500 when trying to save the form (Calc on save only is on) or when trying to create a new form (Calc on save only is off).
The 'timerec___project_title' element is a databasejoin rendered as a dropdown.
Any ideas why this would happen?

Edit: The Error log Shows:
Code:
mod_fcgid: stderr: PHP Notice:  Undefined variable: tableName in /var/www/vhosts/s15402485.onlinehome-server.info/httpdocs/nrw/components/com_fabrik/models/list.php on line 4083, referer: http://s15402485.onlinehome-server.info/nrw/administrator/index.php?option=com_fabrik&view=element&layout=edit&id=8
on saving the calc element, but nothing when i try to save the form.
 
As your signature shows you are using Fabrik 3.0, I am moving this question in the Fabrik 3.0 forum rather than the 3.1 forum.
 
This issue IS regarding a Fabrik 3.1 installation.

I am running 2 sites - the main site is running Fabrik 3.0.8 as stated in my signature. However, for another site I already moved to 3.1.

Sorry for the confusion.
 
Apologies - I have moved this back again.

Line 4083 in the latest list.php is clearly not the same as the one you have installed - can you copy and paste lines c. 4060 to 4110 from your sites list.php so we can see which lines it refers to?

S
 
Code:
    $table = $group->getlistModel()->getTable();
 
        // $$$ hugh - if this is a table-less form ... not much point going any
        // further 'cos things will go BANG
        if (empty($table->id))
        {
            return $return;
        }
        if ($this->isView())
        {
            return $return;
        }
        if ($group->isJoin())
        {
            $tableName = $group->getJoinModel()->getJoin()->table_join;
            $keydata = $this->getPrimaryKeyAndExtra($tableName);
            $keydata = $keydata;
            $primaryKey = $keydata[0]['colname'];
        }
        else
        {
            $keydata = $this->getPrimaryKeyAndExtra($tableName);          // This is Line 4083
            $tableName = $table->db_table_name;
            $primaryKey = $table->db_primary_key;
        }
 
        // $$$ rob base plugin needs to know group info for date fields in non-join repeat groups
        $basePlugIn->setGroupModel($elementModel->getGroupModel());
 
        // The element type AFTER saving
        $objtype = $elementModel->getFieldDescription();
        $dbdescriptions = $this->getDBFields($tableName, 'Field');
 
        if (!$this->canAlterFields() && !$this->canAddFields())
        {
            $objtype = $dbdescriptions[$origColName]->Type;
        }
        if (is_null($objtype))
        {
            return $return;
        }
        $existingfields = array_keys($dbdescriptions);
        $lastfield = $existingfields[count($existingfields) - 1];
        $tableName = FabrikString::safeColName($tableName);
        $lastfield = FabrikString::safeColName($lastfield);
        $altered = false;
        if (!array_key_exists($element->name, $dbdescriptions))
        {
            if ($origColName == '')
            {
                if ($this->canAddFields())
                {
                    $fabrikDb
                    ->setQuery("ALTER TABLE $tableName ADD COLUMN " . FabrikString::safeColName($element->name) . " $objtype AFTER $lastfield");
                    try
                    {
                        $fabrikDb->execute();
                        $altered = true;
                    }
                    catch (Exception $e)
                    {
                        throw new ErrorException('alter structure: ' . $fabrikDb->getErrorMsg(), 500);
                        $altered = false;
                    }
 
                }
            }
            // Commented out as it stops the update when changing an element name
            // return $return;
        }

i posted a little bit more, but marked line 4083.
 
Well we can see the error, can't we.

I have checked the code again, and it looks like Rob fixed this a couple of days ago. So if you update from GitHub, it should now hopefully work.

S
 
I just updated, i cant catch the error in the error_log anymore, but i still get the Servererror 500 on saving the form.
 
I suspect that we have only fixed one problem for another to appear. I think you need Hugh to help with this one.
 
Yes that would be great if someone could have another look at it. I suppose you need an error log? Please tell me which information i should provide to help you
 
if you turn on Joomla debug, and retry to submit, what error message do you get with the 500 error?
 
the joomla debug console is not displayed when i get the Servererror 500. I checked my error_log and somehow this time i could log something regarding this error:
Code:
 mod_fcgid: stderr: PHP Notice:  Undefined variable: formModel in /var/www/vhosts/s15402485.onlinehome-server.info/httpdocs/nrw/plugins/fabrik_element/calc/calc.php(119) : eval()'d code on line 1, referer: http://s15402485.onlinehome-server.info/nrw/index.php/timerec/form/2/21
mod_fcgid: stderr: PHP Fatal error:  Call to a member function getElementData() on a non-object in /var/www/vhosts/s15402485.onlinehome-server.info/httpdocs/nrw/plugins/fabrik_element/calc/calc.php(119) : eval()'d code on line 1, referer: http://s15402485.onlinehome-server.info/nrw/index.php/timerec/form/2/21

maybe this helps? or is there a way to display the debug console while having the Servererror 500?
 
I am not quite sure why you are trying to get the data yourself rather than using a placeholder. Would the following code be equivalent:
Code:
$db = JFactory::getDbo();
$query = "SELECT project_number FROM projects WHERE project_title = '{timerec___project_title}';";
$db->setQuery($query);
$views = $db->loadResult();
return $views;

S
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top