1
我有Drupal 7网站与表events_times
。我需要检查数据库中是否存在任何记录。如果他们这样做,我想要做一个更新而不是一个插入。我一直在修改代码,但无法使其工作。我收到以下错误:Drupal 7 db_update与foreach循环
Fatal error: Call to undefined method UpdateQuery::values()
这里是我的代码:
$times_values= array();
foreach ($form_state['values']['times_fieldset']['registration_times'] as $time) {
$times_values[] = array('event_id' => $eventId,
'registration_times' => $time,
'max_attendees' => 0
);
}
//Check to see if values exist in the table already
$exists = db_select('events_times','et')
->fields('et')
->condition('event_id',$event->id,'=')
->execute()
->fetchAll();
if ($exists == FALSE) {
$query = db_insert('events_times')->fields(array('event_id', 'registration_times', 'max_attendees'));
foreach ($times_values as $record) {
$query->values($record);
}
$query->execute();
}
else {
$query = db_update('events_times')->fields(array('event_id', 'registration_times', 'max_attendees'));
foreach ($times_values as $record) {
$query->values($record);
}
$query->execute();
}
这是设置在我的$ times_values = array();块?在添加调用以检查db和db_update语句之前插入工作 – Johanna
我应该澄清registration_times字段是ajax添加/删除表单对象,因此它一次在db中插入多条记录。 – Johanna
你是对的,插入也接受values()作为方法调用(答案更新)。但更新对象没有值方法(请参阅:https://api.drupal.org/api/drupal/includes!datatabasequery.inc/class/UpdateQuery/7.x),所以是的,你需要替换在循环中的字段(你可能还需要替换查询对象,我最近没有尝试过一个循环中的更新调用 – acrosman