Comments

jweowu’s picture

The patch removes the repetition, and moves the name changes into the query so that we can use a simple fetchAllAssoc('type').

(That does also add 'type' into the settings when it wasn't there before, but that didn't look to be an issue.)

The leading comma formatting in the static query is just my preferred convention for multi-line selects (it means you can comment out lines without worrying about whether the final line has a trailing comma or not), but go ahead and change it to whatever you prefer.

Or you could use a dynamic query. I didn't do that because static queries are slightly faster, and field aliases become a bit more cumbersome (because you need addField()), but in practice the difference is most likely negligible:

  $query = db_select('weight_settings', 'ws');
  $query->addField('ws', 'type');
  $query->addField('ws', 'weight_enabled', 'enabled');
  $query->addField('ws', 'weight_range', 'range');
  $query->addField('ws', 'menu_weight');
  $query->addField('ws', 'weight_default', 'default');
  $query->addField('ws', 'sync_translations');
  if ($type) {
    $query->condition('type', $type);
  }
  $settings = $query->execute()->fetchAllAssoc('type');
jweowu’s picture

Status: Active » Needs review
StatusFileSize
new1.63 KB

Bug fix to handle an unknown $type argument.

jweowu’s picture

StatusFileSize
new1.65 KB

...and it would help if I actually returned arrays (PDO::FETCH_ASSOC) instead of objects :)

davisben’s picture

Status: Needs review » Fixed

I had to add backticks to the column aliases since range and default are reserved words, but everything else looks good. Committed to dev.

jweowu’s picture

Status: Fixed » Needs work

Ah, sorry about that. Perhaps this is why it was done the more long-winded way to begin with. In any case I'm afraid back-ticks are a MySQL-only convention, so we mustn't ever use them with Drupal. It might be sensible to switch to the db_select() approach, and let it take care of the quoting.

davisben’s picture

Status: Needs work » Fixed

I reverted this back to it's previous version.

jweowu’s picture

Fair enough.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.