2

Lately I've been building a Drupal 7 module with the Entity API but I've run in to a problem. When debugging an edit form I've noticed that the load method is called twice which is causing an error.

Recoverable fatal error: Object of class stdClass could not be converted to string in DatabaseStatementBase->execute() (regel 2039 van /drupal7/includes/database/database.inc).

This is caused by FooController::load being executed twice.

Underneath is the code I've been using.

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';
}

And probably the most important part:

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;
  }
  ...
}

Edit: 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  

Has anybody experienced this problem before?

Thanks in advance.

Solution (thanks to @Berdir):

Instead of overriding DrupalDefaultEntityController::load(), using attachLoad():

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

1 Answer 1

2

It looks to me like the second call is actually passing in something invalid (an object instead of int/string).

Instead of that error_log() call, try "debug_print_backtrace()" or if you have devel.module installed, "ddebug_backtrace()". That will give you the callstack to figure out who exactly is calling your load function when. Note that the first command will print a lot of raw text, the second is way easier to read.

Edit: Instead of overriding the load() Method, you should implement attachLoad() instead. That will only be called once for newly loaded entities. See http://api.worldempire.ch/api/privatemsg/privatemsg.module/function/PrivatemsgMessageController::attachLoad/7-1 for an example.

3
  • 1
    I think that is the default behavior that I had to fight with already too. The menu objects are loaded once for the menu and once to get the help system. However, entities should be statically cached anyway and calling foo_load() multiple times should be supported. So the problem is your file_load() line. If it is already loaded, this is what you end up with. I will update my answer with a way to fix it.
    – Berdir
    Mar 9, 2011 at 8:46
  • I am using a quickfix at the moment but obviously I'd rather not use it. The file_load(string) is causing the error on the second load() because it is trying to load the object that is already inserted in the first load(). Seems strange that on the second load() the picture object is already inserted into the foo object. You'd think that it would be selected from the database in parent::load but somehow it's using the object loaded in the first load() (caching?). The lack of documentation on the entity api doesn't help either :).
    – Bart
    Mar 9, 2011 at 9:53
  • See my edit above, you want to use attachLoad() instead of overriding load().
    – Berdir
    Mar 9, 2011 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.