如何以编程方式从节点中删除字段?我在迁移中将hook_update_N
内容从a字段移到自定义表中。迁移之后,我想删除同一函数中的字段。
是否有可以删除字段的字段API?
编辑,解决方案:因为答案缺少实际的代码,所以这是我将$ users中的字段移到我自己的记录中,然后从数据库中删除该字段的方法;
function my_module_update_7005(&$sandbox) {
$slice = 100;
//Fetch users from database;
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_uid'] = 0;
// We'll -1 to disregard the uid 0...
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')->fetchField() - 1;
}
if (empty($users)) {
$sandbox["current_uid"] += $slice;
}
$users = db_select('users', 'u')
->fields('u', array('uid', 'name'))
->condition('uid', $sandbox['current_uid'], '>')
->range(0, $slice)
->orderBy('uid', 'ASC')
->execute();
//Loop trough users;
foreach ($users as $user) {
$foo = new Foo();
// Warning: drupal's fields return mixed values; e.g. NULL versus an int.
$foo->debits = (int) $user->user()->field_credits["und"][0]["value"];
$foo->save();
$sandbox['progress']++;
$sandbox['current_uid'] = $user->uid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
// Remove the field.
field_delete_field("field_credits"); //note that the name for Foo is field_foo
field_purge_batch($sandbox['max']+1);//Drupal seems to have an offbyone problem.
}
field_purge_batch
不过