1

I am getting this error

Fatal error: Call to a member function raw() on a non-object in one of my custom modules.

Following is my function:

function _gocoop_services_get_app_instance($wrapper)
{
  $instance_id = $wrapper->field_seller->field_application_instance->raw();
  if (!$instance_id)
  {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'application_instance')
        ->entityCondition('bundle', 'application_instance')
        ->fieldCondition('field_internal', 'value', 1);
    $result = $query->execute();
    if ($result['application_instance'])
    {
      $instance_id = reset(array_keys($result['application_instance']));
    }
  }
  return $instance_id;
}

Have used the above function at couple of places like shown below:

$wrapper = entity_metadata_wrapper('commerce_product', $entity);
$instance_id = _gocoop_services_get_app_instance($wrapper);

Can anyone help me whats wrong as this has occurred all of a sudden.

  • 1
    wrapper->field_seller->field_application_instance isn't an object, hence the error message. Couldn't say why without proper details of the problem and the data you're using – Clive Mar 27 '15 at 14:34
0

You could try something like this instead, checking if the field value is set first and catching the exception.

function _gocoop_services_get_app_instance($wrapper) {
  $instance_id = FALSE;
  try {
    // Check if wrapper field set first
    if (isset($wrapper->field_seller) && isset($wrapper->field_seller->field_application_instance)) {
      $instance_id = $wrapper->field_seller->field_application_instance->raw();
    }
    if (!$instance_id) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'application_instance')
          ->entityCondition('bundle', 'application_instance')
          ->fieldCondition('field_internal', 'value', 1);
      $result = $query->execute();
      if ($result['application_instance']) {
        $instance_id = reset(array_keys($result['application_instance']));
      }
    }
  } catch (EntityMetadataWrapperException $e) {
    watchdog_exception('gocoop', $e);
  }
  return $instance_id;
}

You might also want to use a static cache for $instance_id, but that would need to vary based on your $wrapper id value.

  • Thanks David. This atleast helped me in getting out of the error. – rav_amadady Mar 28 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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