ezsystems/ezpublish-kernel — pull request #731 by jeromegamez (2014-02-11 20:10:10+00:00)
Title: EZP-22334: Build defined domain fields only

While migrating an eZ Legacy Project, I got the following Error:

ContextErrorException: Notice: Undefined offset: 364 in /var/www/cms/vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/Repository/DomainMapper.php line 118

This is the code in question:

```php
public function buildDomainFields( array $spiFields, ContentType $contentType )
{
$fieldIdentifierMap = array();
foreach ( $contentType->getFieldDefinitions() as $fieldDefinitions )
{
$fieldIdentifierMap[$fieldDefinitions->id] = $fieldDefinitions->identifier;
}

/** @var \eZ\Publish\Core\Repository\FieldTypeService $fieldTypeService */
$fieldTypeService = $this->repository->getFieldTypeService();

$fields = array();
foreach ( $spiFields as $spiField )
{
$fields[] = new Field(
array(
"id" => $spiField->id,
"value" => $fieldTypeService->buildFieldType( $spiField->type )
->fromPersistenceValue( $spiField->value ),
"languageCode" => $spiField->languageCode,
"fieldDefIdentifier" => $fieldIdentifierMap[$spiField->fieldDefinitionId]
)
);
}

return $fields;
}
```

After a little debugging, it seems that the data in the Legacy DB is not perfectly clean. A ContentClass has been reduced by some attributes, but not all corresponding ContentObjects have adapted the change, so that a ContentObject might have more attributes than its Contentclass.

This results in more `$spiFields` than the `$fieldIdentifierMap` will hold, causing a PHP Notice which bubbles to the mentioned Exception, because the `fieldDefinitionId` of the undefined field is not found in `$fieldIdentifierMap`.

Although it should be preferable to have clean database contents, I think it would be a good idea to check if a submitted SPIField is defined in general, and if not, omit it.

This is what this patch introduces.

https://jira.ez.no/browse/EZP-22334

Cheers
:octocat: Jérôme