2

With pure MySQL, I can do something like:

SET @weight:=0;
UPDATE mymodule_table
  SET mymodule_weight=@weight:=@weight+1
  ORDER BY mymodule_weight, mymodule_id;

Is there a Drupal way to achieve similar result? Can I avoid calls to db_query and push it with db_update or db_merge?

MySQL solution adapted from this answer.

1 Answer 1

2

Adding the expression is no problem, but neither UpdateQuery nor MergeQuery implement an orderBy() method. Neither will support setting a local variable first either, as far as I can tell (I'm not even sure a standard query will let you do that actually).

So I think it's safe to say that the answer is no, unless you subclass either UpdateQuery or MergeQuery, and add the sorting and variable declaration functionality yourself.

1
  • 1
    Thanks. I'm disappointed, but at least I wasn't blind not to see how to do it. I will not accept for now, maybe there is a way without variable that can be done "Drupal way". Feel free to remind me after a week or two (but I guess you may be too busy to do that).
    – Mołot
    Oct 15, 2013 at 11:05

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.