如何在多个条件下使用db_delete?

时间:2013-11-05 18:11:34

标签: drupal-7

我正在尝试查询“DELETE FROM ac_journal_term WHERE jid = $ jid AND tid = $ tid”;

foreach ($form_state["values"]["existing_tags"] as $tid)
{
  db_delete('ac_journal_term') 
  ->condition('jid', $jid)->condition('tid', $tid)
  ->execute();
}

我认为这不是正确的方法,但我无法在线找到有关如何使用db_delete执行多个条件的示例。

4 个答案:

答案 0 :(得分:3)

db_delete遵循通常的条件约定。

foreach ($form_state["values"]["existing_tags"] as $tid)
{
  $and = db_and()->condition('jid', $jid)->condition('tid', $tid);
  db_delete('ac_journal_term') 
  ->condition($and)
  ->execute();
}

请参阅条款条款文档:

https://drupal.org/node/310086

答案 1 :(得分:1)

这是block.module中的一个例子:

/**
 * Implements hook_menu_delete().
 */
function block_menu_delete($menu) {
  db_delete('block')
    ->condition('module', 'menu')
    ->condition('delta', $menu['menu_name'])
    ->execute();
  db_delete('block_role')
    ->condition('module', 'menu')
    ->condition('delta', $menu['menu_name'])
    ->execute();
}

答案 2 :(得分:0)

Drupal的做法是 -

db_delete('ac_journal_term') ->condition('jid', $jid) ->condition('tid', $tid) ->execute();

答案 3 :(得分:-1)

<?php
  foreach ($form_state["values"]["existing_tags"] as $tid){
    $and = db_and()->condition('jid', $jid)->condition('tid', $tid);
    db_delete('ac_journal_term')->condition($and)->execute();  
  }
    // Above translates to:
   DELETE FROM {ac_journal_term}  WHERE ( (jid = $jid) AND (tid = $tid) )
?>

如果您需要更多详细信息,请参阅以下链接。 Link