Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storing external data for Legacy storage engine #235

Merged
merged 2 commits into from Mar 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 30 additions & 57 deletions eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php
Expand Up @@ -172,37 +172,32 @@ protected function getEmptyField( FieldDefinition $fieldDefinition, $languageCod
*/
public function createExistingFieldsInNewVersion( Content $content )
{
$fieldsToCopy = array();
$languageCodes = $this->getLanguageCodes( $content->versionInfo->languageIds );
$contentFieldMap = $this->getFieldMap( $content->fields );
$content->fields = array();
$contentType = $this->typeHandler->load( $content->versionInfo->contentInfo->contentTypeId );
$mainLanguageCode = $content->versionInfo->contentInfo->mainLanguageCode;

foreach ( $contentType->fieldDefinitions as $fieldDefinition )
{
foreach ( array_keys( $languageCodes ) as $languageCode )
{
if ( !$fieldDefinition->isTranslatable
&& $languageCode != $content->versionInfo->contentInfo->mainLanguageCode
)
if ( !$fieldDefinition->isTranslatable && $languageCode != $mainLanguageCode )
{
$fieldsToCopy[$fieldDefinition->id][$languageCode] =
$contentFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode];
$createInNewLanguageCode = $languageCode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, what is $createInNewLanguageCode supposed to be for exactly ? I can see it used in the next foreach block, but I'm still a bit confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used on the same level, when calling createExistingFieldInNewVersion(). Used for creating non-translatable field in non-main language, from the field in main language.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haaan I had not seen the two foreaches merged into ones. My bad :-)

$field = $contentFieldMap[$fieldDefinition->id][$mainLanguageCode];
}
else
{
$createInNewLanguageCode = null;
$field = $contentFieldMap[$fieldDefinition->id][$languageCode];
$this->createExistingFieldInNewVersion( $field, $content );
$content->fields[] = $field;
}
}
}

foreach ( $fieldsToCopy as $languageFields )
{
foreach ( $languageFields as $languageCode => $field )
{
$content->fields[] = $this->copyExistingFieldInNewVersion( $field, $languageCode, $content );
$content->fields[] = $this->createExistingFieldInNewVersion(
$field,
$content,
$createInNewLanguageCode
);
}
}
}
Expand Down Expand Up @@ -295,43 +290,6 @@ protected function copyField( Field $originalField, $languageCode, Content $cont
$content->fields[] = $field;
}

/**
* Copies existing field to new field for given $languageCode.
*
* Used by self::createNewFields() and self::updateFields()
*
* @param \eZ\Publish\SPI\Persistence\Content\Field $originalField
* @param string $languageCode
* @param \eZ\Publish\SPI\Persistence\Content $content
*
* @return \eZ\Publish\SPI\Persistence\Content\Field
*/
protected function copyExistingFieldInNewVersion( Field $originalField, $languageCode, Content $content )
{
$field = clone $originalField;
$field->languageCode = $languageCode;
$field->versionNo = $content->versionInfo->versionNo;

$this->contentGateway->insertExistingField(
$content,
$field,
$this->mapper->convertToStorageValue( $field )
);

// If the storage handler returns true, it means that $field value has been modified
// So we need to update it in order to store those modifications
// Field converter is called once again via the Mapper
if ( $this->storageHandler->copyFieldData( $content->versionInfo, $field, $originalField ) === true )
{
$this->contentGateway->updateField(
$field,
$this->mapper->convertToStorageValue( $field )
);
}

return $field;
}

/**
* Updates an existing field in the database.
*
Expand Down Expand Up @@ -362,15 +320,28 @@ protected function updateField( Field $field, Content $content )
}

/**
* Creates an existing field in a new version, no new ID is generated
* Creates an existing field in a new version, no new ID is generated.
*
* If $newLanguageCode is set field will be created in it. This is used for creating non-translatable
* fields from field in main language.
*
* @param Field $field
* External data is being copied here as some FieldTypes require original field external data.
* By default copying falls back to storing, it is upon external storage implementation to override
* the behaviour as needed.
*
* @param Field $originalField
* @param Content $content
* @param string|null $newLanguageCode
*
* @return void
* @return \eZ\Publish\SPI\Persistence\Content\Field
*/
protected function createExistingFieldInNewVersion( Field $field, Content $content )
protected function createExistingFieldInNewVersion( Field $originalField, Content $content, $newLanguageCode = null )
{
$field = clone $originalField;
if ( $newLanguageCode !== null )
{
$field->languageCode = $newLanguageCode;
}
$field->versionNo = $content->versionInfo->versionNo;

$this->contentGateway->insertExistingField(
Expand All @@ -382,13 +353,15 @@ protected function createExistingFieldInNewVersion( Field $field, Content $conte
// If the storage handler returns true, it means that $field value has been modified
// So we need to update it in order to store those modifications
// Field converter is called once again via the Mapper
if ( $this->storageHandler->storeFieldData( $content->versionInfo, $field ) === true )
if ( $this->storageHandler->copyFieldData( $content->versionInfo, $field, $originalField ) === true )
{
$this->contentGateway->updateField(
$field,
$this->mapper->convertToStorageValue( $field )
);
}

return $field;
}

/**
Expand Down
Expand Up @@ -213,7 +213,6 @@ protected function assertCreateExistingFieldsInNewVersion( $storageHandlerUpdate

$callNo = 0;
$fieldValue = new FieldValue();
$fieldsToCopy = array();
foreach ( array( 1, 2, 3 ) as $fieldDefinitionId )
{
foreach ( array( "eng-US", "eng-GB" ) as $languageCode )
Expand All @@ -222,41 +221,26 @@ protected function assertCreateExistingFieldsInNewVersion( $storageHandlerUpdate
array(
"fieldDefinitionId" => $fieldDefinitionId,
"type" => "some-type",
"versionNo" => 1,
"value" => $fieldValue,
"languageCode" => $languageCode
)
);
$originalField = clone $field;
$field->versionNo = 1;
// These fields are copied from main language
if ( ( $fieldDefinitionId == 2 || $fieldDefinitionId == 3 ) && $languageCode == "eng-US" )
{
$originalField = clone $field;
$originalField->languageCode = "eng-GB";
$fieldsToCopy[] = array(
"copy" => clone $field,
"original" => $originalField
);
continue;
}
$storageHandlerMock->expects( $this->at( $callNo++ ) )
->method( 'storeFieldData' )
->method( 'copyFieldData' )
->with(
$this->isInstanceOf( 'eZ\\Publish\\SPI\\Persistence\\Content\\VersionInfo' ),
$this->equalTo( $field )
$this->equalTo( $field ),
$this->equalTo( $originalField )
)->will( $this->returnValue( $storageHandlerUpdatesFields ) );
}
}

foreach ( $fieldsToCopy as $fieldToCopy )
{
$storageHandlerMock->expects( $this->at( $callNo++ ) )
->method( 'copyFieldData' )
->with(
$this->isInstanceOf( 'eZ\\Publish\\SPI\\Persistence\\Content\\VersionInfo' ),
$this->equalTo( $fieldToCopy["copy"] ),
$this->equalTo( $fieldToCopy["original"] )
)->will( $this->returnValue( $storageHandlerUpdatesFields ) );
}
}

/**
Expand Down