-2

I am using Drupal 8 and I am trying to order my query like so:

 $query = db_query("SELECT * FROM `heritage_source_info`WHERE text_id = :textid AND format = :format ORDER BY type ASC", [':textid' => $textid, ':format' => 'text'])->orderBy('FIELD(type,moolam,translation,commentary)');

$available_sources_text = $query->fetchAll();

But it says call to an undefined function orderBy(). Could you help me with the correct syntax?

0

The object returned from db_query() is an instance of a class implementing StatementInterface, which doesn't have any orderBy() method. Since you are using a SQL string, you can order the result adding an ORDER BY clause.

As side note, instead of db_query(), which is deprecated and it's going to be removed from Drupal 9, the code should use \Drupal::database()->query().

The alternative is using \Drupal::database()->select(). In this case, the object returned, which implements SelectInterface, has a orderBy() method.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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