Forums / Developer / Class export and datatype

Class export and datatype

Author Message

Fabien Mas

Tuesday 27 March 2007 5:51:27 am

Hi,

I have created a new class with a new datatype inside.
I have exported it in a package and imported it in a new site.
But each time, the attribute with my new datatype is deleted.
Is it a bug, or I have forgotten to register my new datatype somewhere ?

thx :)

Kristof Coomans

Wednesday 28 March 2007 10:47:56 am

Hi Fabien

First make sure that you set the serialize_supported flag of your datatype to true when you call its parents constructor. I doubt this is really necessary because it doesn't seem to be used somewhere except in the default implementation of serializeContentClassAttribute() in eZDatatype which is pretty useless.

Example from ezauthor:

        $this->eZDataType( EZ_DATATYPESTRING_AUTHOR, ezi18n( 'kernel/classes/datatypes', "Authors", 'Datatype name' ),
                           array( 'serialize_supported' => true ) );

 

Afterwards, there are two ways to get your content serialized and unserialized:

<b>a</b> Let the parent class serialize and unserialize the content by specifying a mapping between attribute database fields and xml elements with the object_serialize_map key.

Example from ezboolean:

        $this->eZDataType( EZ_DATATYPESTRING_BOOLEAN, ezi18n( 'kernel/classes/datatypes', "Checkbox", 'Datatype name' ),
                           array( 'serialize_supported' => true,
                                  'object_serialize_map' => array( 'data_int' => 'value' ) ) );

<b>b</b> Implement the functions serializeContentObjectAttribute and unserializeContentObjectAttribute.

Example from ezauthor, this datatype already stores its value as xml in the database:

    /*!
     \reimp
     \param package
     \param content attribute

     \return a DOM representation of the content object attribute
    */
    function serializeContentObjectAttribute( &$package, &$objectAttribute )
    {
        $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );

        $xml = new eZXML();
        $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
        $node->appendChild( $domDocument->root() );

        return $node;
    }

    /*!
     \reimp

     \param package
     \param contentobject attribute object
     \param ezdomnode object
    */
    function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
    {
        $rootNode = $attributeNode->firstChild();
        $objectAttribute->setAttribute( 'data_text', $rootNode->toString( 0 ) );
    }

To do the same for the class attribute content, implement serializeContentClassAttribute and unserializeContentClassAttribute.

Example from ezboolean:

    /*!
     \reimp
    */
    function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
    {
        $defaultValue = $classAttribute->attribute( 'data_int3' );
        $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
                                                                                 array( 'is-set' => $defaultValue ? 'true' : 'false' ) ) );
    }

    /*!
     \reimp
    */
    function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
    {
        $defaultValue = strtolower( $attributeParametersNode->elementTextContentByName( 'default-value' ) ) == 'true';
        $classAttribute->setAttribute( 'data_int3', $defaultValue );
    }

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Fabien Mas

Thursday 29 March 2007 12:21:04 am

Kristof, thank you for your answer,
I will try this and it should work, I have not serialize anything yet ...

Fabien :)