Forums / Developer / Add object_relation attribute type within a php script

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

eZ debug

Timing: Jan 18 2025 03:01:59
Script start
Timing: Jan 18 2025 03:01:59
Module start 'content'
Timing: Jan 18 2025 03:02:00
Module end 'content'
Timing: Jan 18 2025 03:02:00
Script end

Main resources:

Total runtime0.9615 sec
Peak memory usage4,096.0000 KB
Database Queries224

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 589.0547180.8125
Module start 'content' 0.00590.8319 769.8672771.8125
Module end 'content' 0.83780.1237 1,541.6797348.4297
Script end 0.9615  1,890.1094 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00380.3951210.0002
Check MTime0.00140.1480210.0001
Mysql Total
Database connection0.00100.099910.0010
Mysqli_queries0.862489.69242240.0039
Looping result0.00220.22932220.0000
Template Total0.933197.020.4665
Template load0.00210.216920.0010
Template processing0.931096.822620.4655
Template load and register function0.00020.022910.0002
states
state_id_array0.00120.122310.0012
state_identifier_array0.00130.135520.0007
Override
Cache load0.00190.2010970.0000
Sytem overhead
Fetch class attribute can translate value0.00160.170160.0003
Fetch class attribute name0.00140.1450130.0001
XML
Image XML parsing0.00200.203160.0003
class_abstraction
Instantiating content class attribute0.00000.0036160.0000
General
dbfile0.00430.4495330.0001
String conversion0.00000.000730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
11content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
28content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
12content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
9content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
5content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 67
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs