Problem/Motivation

I am getting a PHP notice for undefined object property "original" when saving a field collection using $fc_item->save(TRUE) where $skip_host_save = TRUE.

Notice: Trying to get property of non-object in EntityAPIController->saveRevision() (line 520 of /entity/includes/entity.controller.inc).

This calls entity_get_controller($this->entityType)->save($this) and passes my entity object (seen below) to saveRevision($entity) in entity_controller.inc.

It is reporting the error on the following line:

$update_default_revision = $entity->{$this->defaultRevisionKey} && $entity->{$this->revisionKey} != $entity->original->{$this->revisionKey}

I cannot use the entity_meta_wrapper as it does not provide the $skip_host_save argument. Therefore I modify field collection properties in code as shown below and call save method directly on the field collection entity object.

$fc_item->{$field_name}[LANGUAGE_NONE][0]['value'] = $field_value;
$fc_item->save(TRUE);

In this senario $entity->original does not exist in certain cases and then we run into this issue.

Proposed resolution

Determine if either of the below are required, or both.

  1. if field_collection should force inclusion of $entity->original object
  2. if a check should be provided in entity.controller.inc

Remaining tasks

  1. if field_collection should force inclusion of $entity->original object
  2. if a check should be provided in entity.controller.inc - patch provided

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

mccrodp created an issue. See original summary.

mccrodp’s picture

Issue summary: View changes
mccrodp’s picture

Title: PHP Notice "Trying to get property of non-object" when calling $fc_item->save(TRUE) » PHP Notice as $entity->original may not exist when calling save on a field collection entity
Project: Field collection » Entity API
Version: 7.x-1.0-beta10 » 7.x-1.x-dev
Component: Code » Code - misc
Issue summary: View changes
mccrodp’s picture

Issue summary: View changes
StatusFileSize
new1.06 KB

Adding a patch to check for existence of $entity->original property.

Changed:

$update_default_revision = $entity->{$this->defaultRevisionKey} && $entity->{$this->revisionKey} != $entity->original->{$this->revisionKey};

To:

if (!empty($entity->original)) {
        $revision_key_match = $entity->{$this->revisionKey} != $entity->original->{$this->revisionKey};
      }
      else {
        $revision_key_match = FALSE;
      }
      $update_default_revision = $entity->{$this->defaultRevisionKey} && $revision_key_match;
mccrodp’s picture

Status: Active » Needs review
chris matthews’s picture

The 3 year old patch in #4 to entity.controller.inc applied cleanly to the latest entity 7.x-1.x-dev and (if still relevant) needs review.