1

Very simple query:

    $query = $db->getQuery(true)
        ->select('*')
        ->from('#__mail_templates')
        ->where('template_id = ' . $db->quote($this->getRowId()))
        ->where('language = ' . $db->quote($languageCode));
    $rows = $db->loadObjectList($query);

For some ungodly reason, $rows has index "", empty string. Using Joomla 4.2. I hope this is not intended behaviour??

var dump:

array(1) {
  [""]=>
  object(stdClass)#218 (14) {
    ["lang_id"]=>
    int(2)
    ["asset_id"]=>
    int(66692)
    ["lang_code"]=>
    string(5) "de-DE"
    ...

Oh, I see now that the result is not coming from the correct table even.

#message: "Unknown fetch type '8'"
1
  • Was this the result set from the previously executed query?
    – mickmackusa
    Mar 1 at 18:11

2 Answers 2

2

We can improve queries with prepared statements. As example:

$query = $db
    ->getQuery(true)
    ->select('*')
    ->from($db->qn('#__mail_templates'))
    ->where($db->qn('template_id') . ' = :getRowId')
    ->where($db->qn('language') . ' = :languageCode')
    ->bind(':getRowId', $this->getRowId())
    ->bind(':languageCode', $languageCode);
    
$db->setQuery($query);
        
$rows = $db->loadObjectList();

https://docs.joomla.org/J4.x:Moving_Joomla_To_Prepared_Statements

5
1

Meh. Buggy line should be

$rows = $db->setQuery($query)->loadObjectList();

A warning would have been nice...

2
  • A simple mistake like this is likely to bite other Joomlers; this is a good addition to our Q&A.
    – mickmackusa
    Mar 1 at 18:09
  • 1
    @mickmackusa Can't hurt. :) Mar 1 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.