protected function fixInputRelations(eZContentObject $object, $versionNo)
    {
        $validRelationTypes = array(1, 2, 4, 8, 16, 32, 64, 128);

        $db = eZDB::instance();
        $rows = $db->arrayQuery(
            "SELECT * FROM ezcontentobject_link
            WHERE from_contentobject_id={$object->attribute('id')}
            AND from_contentobject_version={$versionNo}"
        );

        foreach ($rows as $row) {
            $relationType = (int)$row['relation_type'];

            if (!in_array($relationType, $validRelationTypes)) {
                foreach ($validRelationTypes as $validRelationType) {
                    if ($relationType & $validRelationType) {
                        $db->query(
                            "INSERT INTO ezcontentobject_link (
                                from_contentobject_id,
                                from_contentobject_version,
                                to_contentobject_id,
                                contentclassattribute_id,
                                relation_type
                            ) VALUES (
                                {$row['from_contentobject_id']},
                                {$row['from_contentobject_version']},
                                {$row['to_contentobject_id']},
                                {$row['contentclassattribute_id']},
                                {$validRelationType}
                            )
                        ");
                    }
                }

                $db->query("DELETE FROM ezcontentobject_link WHERE id = {$row['id']}");
            }
        }
    }