Getting error from Calc Element on a new form entry

smart

Member
Hello again.

I have a calc element with this codes below. List views and individual form view shows the correct result for that calc element. But when the new entry (form) is created, this error shows up.
0 syntax error, unexpected ')'
I am searching through the forum but hasn't found the correction solution yet.
Any help would be appreciated. Thanks
PHP:
if (!empty('{book___btid_raw}')) {
  $myDb = FabrikWorker::getDbo(false, 2);
  $myQuery = $myDb->getQuery(true);
  $myQuery->select("tnamee")->from("teacher")->where("tid = ". {book___btid_raw});
  $myDb->setQuery($myQuery);
  return $myDb->loadResult();
}
return '';
 
I suspect it is the first line. PHP "empty" function is intended to take a variable name as the parameter not a string. I suspect that this works when the string contains a value, but not when the placeholder is empty.

Instead, why not try:
PHP:
if ('{book___btid_raw}' != '')
 
Thanks Sophist.
It didn't work. I tried several variation even using $formModel->getElementData and not working.
As far as I understand, during the form load, the dbjoin element raw value would be NULL or empty. If there is only "IF" statement checking the empty, there is no error. But if there are that 5 lines code, it produces the error.
I admit I am not expert in php as well as fabrik. Still learning.
 
Hi Sophist,
Thanks. I think I find the error. Single quotes are needed for the place holder.
I am double checking now.
PHP:
  $myQuery->select("tnamee")->from("teacher")->where("tid = ". {book___btid_raw});
should be
PHP:
  $myQuery->select("tnamee")->from("teacher")->where("tid = ". '{book___btid_raw}');
 
Yes - obvious now you mention it. You think that the code is not executed when the placeholder is empty, but the placeholders are actually substituted into the php source code, so without the quotes when the placeholder is empty the code that is evaluated is actually:
PHP:
$myQuery->select("tnamee")->from("teacher")->where("tid = " . );
And now you can see why there is an unexpected ")".
 
I'd also add a $db->quote() or (as this seems to be a numeric id) cast it to an int, in there as well, so you don't wind up with a similar issue in the query itself ...

$myQuery->select("tnamee")->from("teacher")->where("tid = ".$myDb->quote('{book___btid_raw}'));

... or ...

$myQuery->select("tnamee")->from("teacher")->where("tid = ". (int)'{book___btid_raw}');

... or even just put the placeholder within the where string ...

$myQuery->select("tnamee")->from("teacher")->where("tid = '{book___btid_raw}'");

Otherwise you would end up with a query like ...

WHERE tid =

... which would throw a MySQL error, rather than ...

WHERE tid = ''

... or ...

WHERE tid = 0

-- hugh
 
Thanks, Cheesegrits. Your advice will be very helpful for this and the future queries as well.
I appreciate it. :)
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top