eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter::toStorageValue PHP Method

toStorageValue() public method

Converts data from $value to $storageFieldValue.
public toStorageValue ( eZ\Publish\SPI\Persistence\Content\FieldValue $value, StorageFieldValue $storageFieldValue )
$value eZ\Publish\SPI\Persistence\Content\FieldValue
$storageFieldValue eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
    {
        $doc = new DOMDocument('1.0', 'utf-8');
        $root = $doc->createElement('related-objects');
        $doc->appendChild($root);
        $relationList = $doc->createElement('relation-list');
        $data = $this->getRelationXmlHashFromDB($value->data['destinationContentIds']);
        $priority = 0;
        foreach ($value->data['destinationContentIds'] as $id) {
            if (!isset($data[$id][0])) {
                // Ignore deleted content items (we can't throw as it would block ContentService->createContentDraft())
                continue;
            }
            $row = $data[$id][0];
            $row['ezcontentobject_id'] = $id;
            $row['priority'] = $priority += 1;
            $relationItem = $doc->createElement('relation-item');
            foreach (self::dbAttributeMap() as $domAttrKey => $propertyKey) {
                if (!isset($row[$propertyKey])) {
                    // left join data missing, ignore the given attribute (content in trash missing location)
                    continue;
                }
                $relationItem->setAttribute($domAttrKey, $row[$propertyKey]);
            }
            $relationList->appendChild($relationItem);
            unset($relationItem);
        }
        $root->appendChild($relationList);
        $doc->appendChild($root);
        $storageFieldValue->dataText = $doc->saveXML();
    }

Usage Example

    /**
     * @group fieldType
     * @group relationlist
     */
    public function testToStorageValueEmpty()
    {
        $destinationContentIds = array();
        $fieldValue = new FieldValue();
        $fieldValue->sortKey = false;
        $fieldValue->data = array("destinationContentIds" => $destinationContentIds);
        $expectedStorageFieldValue = new StorageFieldValue();
        $expectedStorageFieldValue->dataText = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<related-objects><relation-list/></related-objects>

EOT;
        $actualStorageFieldValue = new StorageFieldValue();
        $this->converter->expects($this->once())->method("getRelationXmlHashFromDB")->with($destinationContentIds)->will($this->returnValue(array()));
        $this->converter->toStorageValue($fieldValue, $actualStorageFieldValue);
        $this->assertEquals($expectedStorageFieldValue, $actualStorageFieldValue);
    }