Add object_relation attribute type within a php script

Author Message

Fabio Carissimi

Wednesday 21 June 2006 6:30:55 am

Hello,

in this thread, it is explained how to update an entry of an object with a php script:
http://ez.no/community/forum/developer/importing_update_entry_if_it_already_exists

With similar methods, I know how to create a new object in a script with attributes of early any kind of datatypes.

My problem is how to set an 'object relation' attribute?

To add an object relation at the contentobject level, I can use this function :

eZContentObject::addContentObjectRelation  ($toObjectID,
                                                                    $fromObjectVersion = false,
                                                                    $fromObjectID = false,
                                                                    $attributeID = 0)

But this function add a relation between 2 distinct objects,
What i want is to set an attribute of type 'object_relation' or 'object_relation_list'.

To modify an attribute of "simple" datatype I can do something like :

$contentObjectAttribute->setAttribute("data_text", "My new piece of information");
$contentObjectAttribute->store();

with 'object_relation' instead of "data_text".

the function addContentObjectRelation() has a $attributeID parameter which is described like that :
ID of class attribute.
IF > 0 - relations made with attribute ID ("related object(s)" datatype)
0 - regular relations (content object level)

So it seems that I have to use addContentObjectRelation() with the right $attributeID parameter to add
a relation at attribute level, but where can I find the mapping between attributeID and my attribute ?
Am I right ?
And is it enough ? should'nt I also use something like $contentObjectAttribute->setAttribute(...) ?

Many thanks for help

Kristof Coomans

Wednesday 21 June 2006 11:05:11 am

Hi Fabio

Some example code to add objects to an attribute with the datatype object relation list:

$content = $objectRelationListAttribute->content();
$priority = 1;

foreach ( $objectsToRelate as $objectToRelate )
{
    $content['relation_list'][] = eZObjectRelationListType::appendObject( $objectToRelate->attribute( 'id' ), $priority, $objectRelationListAttribute );
    $objectRelationListAttribute->setContent( $content );
    $objectRelationListAttribute->store();
    $priority++;
}

For an attribute with the object relation datatype, you can use:

$objectRelationAttribute->setAttribute( 'data_int', $objectToRelate->attribute( 'id' ) );
$objectRelationAttribute->store();

Content object relations on attribute level will also be made when you use the code above (the datatypes will take care of it).

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

Fabio Carissimi

Thursday 29 June 2006 5:45:08 am

Hi

thanks for this usefull answer,

I tried the solution for object relation :

$objectRelationAttribute->setAttribute( 'data_int', $objectToRelate->attribute( 'id' ) );
$objectRelationAttribute->store();

And it works fine.

But I can make the solution for object relation list work,
I use a code similar to your suggestion and when I print out the content of $content['relation_list']

$cli->output(print_r($content['relation_list']));


the array is complete and it seems to work,

but when I come bakc to the admin interface, the object is created without the object relation list.

Is there something else I have to add ?

Many thanks
Fabio

Kristof Coomans

Tuesday 04 July 2006 2:53:05 am

Hi Fabio

Does the object attribute contains the relations and the relation list doesn't? Or do the relations show up nowhere?

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

Fabio Carissimi

Tuesday 04 July 2006 7:59:08 am

Hi Kristoff,
here is the result of $cli->output(print_r($content)); after adding relations,
it seems that $content['relation_list'] is updated (like I want) but in the admin interface I can see no relations.

Array
(
    [type] => 2
    [class_constraint_list] => Array
        (
        )
    [default_placement] =>
    [relation_list] => Array
        (
            [0] => Array
                (
                    [identifier] =>
                    [priority] => 1
                    [contentobject_id] => 407
                    [contentobject_version] => 1
                    [node_id] => 401
                    [parent_node_id] => 60
                    [contentclass_id] => 19
                    [contentclass_identifier] => theme_recherche
                    [is_modified] =>
                    [object] => ezcontentobject Object
                        (
                            [PersistentDataDirty] =>
                            [ID] => 407
                            [Name] => Instrumentation, Robotique, Images, Signaux (IRIS)
                            [CurrentLanguage] => fre-FR
                            [ClassName] =>
                            [ClassIdentifier] =>
                            [DataMap] => Array
                                (
                                )
                            [ContentActionList] =>
                            [ContentObjectAttributes] => Array
                                (
                                )
                            [MainNodeID] => 401
                            [SectionID] => 1
                            [OwnerID] => 14
                            [ClassID] => 19
                            [IsPublished] => 0
                            [Published] => 1152023793
                            [Modified] => 1152023793
                            [CurrentVersion] => 1
                            [Status] => 1
                            [RemoteID] => b782cbf058d06ee575c3d1b2cbc335ba
                            [LanguageMask] => 2
                            [InitialLanguageID] => 2
                        )
                )
        )
)

Xavier Dutoit

Tuesday 04 July 2006 9:05:10 am

Salut Fabio,

It looks like you put the complete node, you should just put its object id.

X+

http://www.sydesy.com

Fabio Carissimi

Tuesday 11 July 2006 2:02:23 am

Hello,

I did not find exactly why but I have rewrited all the script step by step and your method works
for objetc_relation and object_relation_list.

Many thanks for help.
Fabio

Kristof Coomans

Friday 14 July 2006 11:09:03 am

Nice to hear it worked :-)

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

*- pike

Sunday 29 October 2006 7:54:10 am

nice thread

ofcourse, to use eZObjectRelationListType::appendObject you need to have included /kernel/classes/datatypes/ezobjectrelationlist/ezobjectrelationlisttype.php somewhere.

it seems rare to include that file directly. more often, people seem to include

include_once( 'kernel/classes/ezdatatype.php' );

and do something like

eZDataType::loadAndRegisterAllTypes();

which includes ezobjectrelationlisttype.php if it is a valid type.

$2c,
*pike

PS. the print_r output of Fabio Carissimi seems ok. The object get's included in $content['relation_list'][0]['object'] by the appendObject call.

---------------
The class eZContentObjectTreeNode does.

Kedar Deshpande

Friday 06 November 2009 11:25:07 am

I used the code Kristof mentioned up above (with variable changes) but it seems to crash at the line where the appendObject method is called:

<code>

$content = $positionAttributes['article_selection']->content();
$priority = 1;
$content['relation_list'][] = eZObjectRelationListType::appendObject( $articleObject->attribute( 'id' ), $priority, $positionAttributes['article_selection'] );
$positionAttributes['article_selection']->setContent( $content );
$positionAttributes['article_selection']->store();
$priority++;

</code>

Any ideas why?

Thanks.

Kristof Coomans

Saturday 07 November 2009 6:30:21 am

Hi Kedar

Can you show us which error you get?

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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.