Forums / Developer / eZObjectRelationListType not working as expected in 4.0 ?

eZObjectRelationListType not working as expected in 4.0 ?

Author Message

Damarev .

Thursday 21 February 2008 2:55:03 am

Hi. I want to remove and append relations from a user attribute (I have modified the user class to meet my needs, adding an "object relations" datatype).

This is my code in the module:

$uid =& eZUser::currentUserID();
$user =& ezUser::fetch( $uid , true );
$contentObject =& $user->contentObject();
$contentObjectAttributes =& $contentObject->ContentObjectAttributes();
$actosObject =& $contentObjectAttributes[6];
$actosContent =& $contentObjectAttributes[6]->content();

print_r($actosContent);

$contentFileObjectID = 3264;
$priority = 999;
$actosContent['relation_list'][] = eZObjectRelationListType::appendObject( $contentFileObjectID, $priority, $actosObject );

print_r($actosContent);

If I print the contents of $actosContent before and after, I get:

Before:

Array
(
    [relation_list] => Array
        (
            [0] => Array
                (
                    [priority] => 1
                    [contentobject_id] => 3201
                    [contentobject_version] => 2
                    [node_id] => 3176
                    [parent_node_id] => 3174
                    [contentclass_id] => 43
                    [contentclass_identifier] => acto
                    [identifier] => 
                    [is_modified] => 
                )
        )
)

After eZObjectRelationListType::appendObject :

Array
(
    [relation_list] => Array
        (
            [0] => Array
                (
                    [priority] => 1
                    [contentobject_id] => 3201
                    [contentobject_version] => 2
                    [node_id] => 3176
                    [parent_node_id] => 3174
                    [contentclass_id] => 43
                    [contentclass_identifier] => acto
                    [identifier] => 
                    [is_modified] => 
                )

            [1] => Array
                (
                    [identifier] => 
                    [priority] => 999
                    [contentobject_id] => 3264
                    [contentobject_version] => 1
                    [node_id] => 3175
                    [parent_node_id] => 3174
                    [contentclass_id] => 43
                    [contentclass_identifier] => acto
                    [is_modified] => 
                    [object] => ezcontentobject Object
                        (
                            [PersistentDataDirty] => 
                            [ID] => 3264
                            [Name] => Acto 01
                            [CurrentLanguage] => esl-ES
                            [ClassName] => 
                            [ClassIdentifier] => 
                            [DataMap] => Array
                                (
                                )

                            [ContentActionList] => 
                            [ContentObjectAttributes] => Array
                                (
                                )

                            [MainNodeID] => 3175
                            [SectionID] => 11
                            [OwnerID] => 14
                            [ClassID] => 43
                            [IsPublished] => 0
                            [Published] => 1203505438
                            [Modified] => 1203505438
                            [CurrentVersion] => 1
                            [Status] => 1
                            [RemoteID] => 95e91d5c7525f97f334a6291c7a1d706
                        )
                )
        )
)

Dont know why the second object relation has an "ezcontentobject Object" inside... is this correct?

Dont know either if I should do:

$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $uid,'version' => 1 ) );

or

$contentObjectAttributes[6]->setContent( $actosContent );
$contentObjectAttributes[6]->store();

or both in order to save the changes?

BTW, how can I REMOVE a relation? Im trying this but has no effect:

eZObjectRelationListType::removeRelatedObjectItem( $actosObject , 3266 );

Thanks.

Mickael Robin

Thursday 19 February 2009 3:44:44 am

Hi all,
I second the last question : how can I REMOVE a relation ?

I managed to use the "eZObjectRelationListType::appendObject" function, but it doesn't seem to work when I replace this function with "eZObjectRelationListType::removeRelatedObjectItem" .

I put my code below in case someone would find the problem...

...
else if ( $http->hasPostVariable( "removegenre" ) )
{
$object_membre_origine_id = $http->postVariable( 'object_membre_origine_id' );
$object_genre_cible_id = $http->postVariable( 'object_genre_cible_id' );
$redirect_node_id = $http->postVariable( 'redirect_node_id' );
	
$object_membre_origine = eZContentObject::fetch( $object_membre_origine_id );

$contentObjectVersion = $object_membre_origine->version( $object_membre_origine->attribute( 'current_version' ) );
$contentObjectAttributes = $contentObjectVersion->contentObjectAttributes();

	foreach ($contentObjectAttributes as $contentObjectAttribute)
	{
	   $contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );
 
	   if ($contentClassAttribute->attribute("identifier") == "genres")
	   {   	
	   	
$content = $contentObjectAttribute->content();
		$priority = '1';
		
		   $content['relation_list'][] = eZObjectRelationListType::removeRelatedObjectItem( $contentObjectAttribute, $object_genre_cible_id );
		   $contentObjectAttribute->setContent( $content );
		   $contentObjectAttribute->store();
		   $priority++;		   
	   }
	}
return $module->redirectToView( 'view', array( 'full', $redirect_node_id ) ) ;
}
...

Thanks in advance for your help :-)

Mickael Robin

Thursday 19 February 2009 4:25:29 am

OK, my mistake.

Those 2 functions do not work the same way : removeRelatedObjectItem does not apply on a ['relation_list'] array but on the whole "$contentObjectAttribute->content()".

With this modification, I can now remove relations.
For info, the object relation is removed directly in the database, which seems to lead to "possible edit conflicts".No big deal but still annoying....

Mickael Robin

Thursday 26 February 2009 6:36:43 am

Hi again,
finally, I can remove the relation_item in the database, but the new content of the "data_text" attribute of the $contentObjectAttribute does not appear on the site (even after emptying cache) ; so that I have to create a new version from the admin interface to have the attribute content displayed.

I tried to complete the script with the "fixRelatedObjectItem" function, without success.
Here's my code :

else if ( $http->hasPostVariable( "removeambiance" ) )
{
$object_membre_origine_id = $http->postVariable( 'object_membre_origine_id' );
$object_ambiance_cible_id = $http->postVariable( 'object_ambiance_cible_id' );
$redirect_node_id = $http->postVariable( 'redirect_node_id' );
	
$object_membre_origine = eZContentObject::fetch( $object_membre_origine_id );

$contentObjectVersion = $object_membre_origine->version( $object_membre_origine->attribute( 'current_version' ) );
$contentObjectAttributes = $contentObjectVersion->contentObjectAttributes();

	foreach ($contentObjectAttributes as $contentObjectAttribute)
	{
	   $contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );
 
	   if ($contentClassAttribute->attribute("identifier") == "ambiances")
	   {   	
//		$priority = '1';
		    $content = eZObjectRelationListType::removeRelatedObjectItem( $contentObjectAttribute, $object_ambiance_cible_id );
  			$contentObjectAttribute->setContent( $content );
 			$contentObjectAttribute->store();
//		   $priority++;		   
  
// test adding the "fix" 
eZObjectRelationListType::fixRelatedObjectItem ( $contentObjectAttribute, $object_ambiance_cible_id, 'remove' );	   
	   }
	}
return $module->redirectToView( 'view', array( 'full', $redirect_node_id ) ) ;
}

I can't figure out how this script can set the good value for the attribute in the database while preventing any or the related_items to display...?

This might be related to these debug errors, which I unfortunately don't understand :

Strict: PHP 	Feb 26 2009 12:11:37
Non-static method eZObjectRelationListType::removeRelatedObjectItem() should not be called statically, assuming $this from incompatible context in C:\mysite\content\action.php on line 1622

Strict: PHP 	Feb 26 2009 12:11:37
Non-static method eZObjectRelationListType::storeObjectDOMDocument() should not be called statically, assuming $this from incompatible context in C:\mysite\kernel\classes\datatypes\ezobjectrelationlist\ezobjectrelationlisttype.php on line 1824

I'm kind of stuck at the very end of my project. Any help would then be greatly appreciated, even a bulk piece of code using the "removeRelatedObjectItem" function...

Thanks in advance :-)

André R.

Thursday 26 February 2009 7:49:12 am

Searching for ">removeRelatedObjectItem(" (> since it is a non strict method, hence why you get those strict warnings) gave me 2 hits:
ezcontentobject.php line ~1637:

        // Remove references from XML.
        if ( count( $result ) > 0 )
        {
            foreach( $result as $row )
            {
                $attr = new eZContentObjectAttribute( $row );
                $dataType = $attr->dataType();
                $dataType->removeRelatedObjectItem( $attr, $objectID );
                eZContentCacheManager::clearObjectViewCache( $attr->attribute( 'contentobject_id' ), true );
                $attr->storeData();
            }
        }

ezobjectrelationlisttype.php line ~1265:

    function fixRelationsRemove ( $objectID, $contentObjectAttribute )
    {
        $this->removeRelatedObjectItem( $contentObjectAttribute, $objectID );
        $contentObjectAttribute->storeData();
    }

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom