实体方法加载两次

时间:2011-03-07 08:33:28

标签: drupal drupal-entities

最近我一直在用Entity API构建一个Drupal 7模块,但是我遇到了一个问题。在调试编辑表单时,我注意到load方法被调用两次导致错误。

  

可恢复的致命错误:对象   class stdClass无法转换   串入   DatabaseStatementBase->执行()   (regel 2039 van   /drupal7/includes/database/database.inc)。

这是由FooController :: load执行两次引起的。

下面是我一直在使用的代码。

function foo_menu() {
  $items = array();
  ...
  $items['admin/foo/%foo/edit'] = array(
    'title' => 'Edit foo',
    'page callback' => 'foo_edit',
    'page arguments' => array(2),
    'access arguments' => array('administer content'),
    'type' => MENU_CALLBACK,
  );
  ...
  return $items;
}

function foo_edit($foo) {
  return drupal_get_form('foo_edit_form', $foo);
}

function foo_edit_form($form, &$form_state, $foo) {
  $form['#foo'] = $foo;
  $form_state['values'] = $foo;

  $form['id'] = array(
    '#type' => 'hidden',
    '#value' => $foo->id,
  );
  ...
  $form['picture'] = array(
    '#type' => 'file',
    '#title' => t('Picture'),
    '#default_value' => $foo->picture->filename,
  );
  ...
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

function foo_edit_form_submit($form, &$form_state) {
  $foo = (object)$form_state['values'];

  if ($picture = file_save_upload('picture')) {
    $picture = file_move($picture, FOO_FILE_PATH . $picture->filename, FILE_EXISTS_RENAME);
    $picture->status |= FILE_STATUS_PERMANENT;
    $picture = file_save($picture);      
    $foo->picture = $picture->fid;
  }

  entity_get_controller('foo')->save($foo);

  drupal_set_message(t('Foo saved.'));
  $form_state['redirect'] = 'admin/foo';
}

可能是最重要的部分:

function foo_load ($id) {
  $foo = foo_load_multiple(array($id));
  return $foo ? $foo[$id] : FALSE;
}
function foo_load_multiple ($ids = array(), $conditions = array()) {
  return entity_load('foo', $ids, $conditions);
}
class FooController extends DrupalDefaultEntityController {
  ...     
  function load ($ids = array(), $conditions = array()) {
    $foos = parent::load($ids, $conditions);

    // Code executed twice
    error_log('FooController::load');

    foreach ($foos as $foo) {
      $foo->picture = file_load($foo->picture);
    }
    return $foos;
  }
  ...
}

编辑:callstack

... (Array, 11 elements)  
    11: load (Array, 7 elements)  
    10: entity_load (Array, 4 elements)  
    9: foo_load_multiple (Array, 4 elements)  
    8: foo_load (Array, 4 elements)  
    7: _menu_load_objects (Array, 4 elements)  
    6: _menu_translate (Array, 4 elements)  
    5: menu_get_item (Array, 4 elements)  
    4: menu_get_custom_theme (Array, 4 elements)  
    3: menu_set_custom_theme (Array, 4 elements)  
    2: _drupal_bootstrap_full (Array, 4 elements)  
    1: drupal_bootstrap (Array, 4 elements)  
Called from /sites/bar/modules/custom/foo/foo.module, line 367  

... (Array, 20 elements)  
    20: load (Array, 7 elements)  
    19: entity_load (Array, 4 elements)  
    18: foo_load_multiple (Array, 4 elements)  
    17: foo_load (Array, 4 elements)  
    16: _menu_load_objects (Array, 4 elements)  
    15: _menu_translate (Array, 4 elements)  
    14: menu_local_tasks (Array, 4 elements)  
    13: menu_tab_root_path (Array, 4 elements)  
    12: menu_get_active_help (Array, 4 elements)  
    11: system_block_view (Array, 2 elements)  
    10: call_user_func_array (Array, 4 elements)  
    9: module_invoke (Array, 4 elements)  
    8: _block_render_blocks (Array, 4 elements)  
    7: block_list (Array, 4 elements)  
    6: block_get_blocks_by_region (Array, 4 elements)  
    5: block_page_build (Array, 4 elements)  
    4: drupal_render_page (Array, 4 elements)  
    3: drupal_deliver_html_page (Array, 4 elements)  
    2: drupal_deliver_page (Array, 4 elements)  
    1: menu_execute_active_handler (Array, 4 elements)  
Called from /sites/bar/modules/custom/foo/foo.module, line 367  

以前有人遇到过这个问题吗?

提前致谢。

解决方案(感谢@Berdir):

而不是使用attachLoad()重写DrupalDefaultEntityController :: load():

protected function attachLoad(&$foos, $revision_id = FALSE) {
    foreach ($foos as $foo) {
      $foo->picture = file_load($foo->picture);
    }
    parent::attachLoad($foos, $revision_id);
  }

1 个答案:

答案 0 :(得分:2)

在我看来,第二个调用实际上传递了一些无效的东西(一个对象而不是int / string)。

尝试“debug_print_backtrace()”或者如果安装了devel.module,而不是调用error_log(),“ddebug_backtrace()”。这将为您提供callstack,以确定何时正在调用您的加载函数。请注意,第一个命令将打印大量原始文本,第二个命令将更容易阅读。

修改: 您应该实现attachLoad(),而不是覆盖load()方法。对于新加载的实体,只会调用一次。有关示例,请参阅http://api.worldempire.ch/api/privatemsg/privatemsg.module/function/PrivatemsgMessageController::attachLoad/7-1