Forums / Developer / [Solved] Modify/Update objects from PHP

[Solved] Modify/Update objects from PHP

Author Message

Damien MARTIN

Wednesday 20 January 2010 2:14:41 am

Hi there,

I would like to know how can we do, to modify existing objects directly in PHP ?

To create Nodes It's really simple using eZContentFunctions::createAndPublishObject (thanks to Damien Pobel for the tip : http://pwet.fr/blog/comment_creer_des_objets_ez_publish_en_php )

Is there a simple way to update nodes like this ?

Otherwise, does someone have a method to do that ?

I need to update selected fields in my class and to store my modifications.

It is for a cronjob.

Thanks,

Damien

Gaetano Giunta

Wednesday 20 January 2010 3:01:04 am

take a look at data import extension - it has very clean code to do that

Principal Consultant International Business
Member of the Community Project Board

Damien MARTIN

Wednesday 20 January 2010 5:51:29 am

Thank you Gaetano,

Thanks to ImportOperator::save_eZ_attribute, I discovered what I was waited for :

$contentObjectAttribute->fromString( $value );
$contentObjectAttribute->store();

Where $contentObjectAttribute is retrieved from :

$object->DataMap();

after a simple :

$object = eZContentObject::fetchByNodeID($node_id);

The values are correctly updated !

Thank you again.

Laurent Dorier

Monday 25 January 2010 12:52:46 am

Hello,

I'm trying to do the same (not for a cronjob but for an eZ extension) but unsuccesful...

$object= eZContentObject::fetchByNodeID($node_id);
$dataMap = $object->dataMap();
$contentObjectAttribute = ImportOperator::save_eZ_attribute($dataMap['MyObject_relation_list']);
$value = '206-207-213';
$contentObjectAttribute->fromString( $value );
$contentObjectAttribute->store();

I get:

Non-static method ImportOperator::save_eZ_attribute() should not be called statically

Someone can help ?

Damien MARTIN

Monday 25 January 2010 1:12:44 am

Hi Laurent,

I have not used directly the classes from the extensions but I have extract it to my own class.

save_eZ_attribute is not static so, you should call it from an ImportOperator instance :

function save_eZ_attribute( $contentObjectAttribute )

Otherwise, this my version of the method :

/**
 * Sauve un attribut eZ d'un objet avec une nouvelle valeur.
 * Cette méthode à l'avantage de gérer complétement les ObjectRelations et les RelationList.
 * \arg $contentObjectAttribute Pointeur sur un attribut d'un objet
 * \arg $value La valeur au format TXT
 * \author Mugo Web Copyright (C) 2008 => extension data_import (http://projects.ez.no/data_import)
 */
function save_eZ_attribute( $contentObjectAttribute, $value )
{
    
    switch( $contentObjectAttribute->attribute( 'data_type_string' ) )
    {
        case 'ezobjectrelation':
        {
            // Remove any exisiting value first from ezobjectrelation
            /*
            eZContentObject::removeContentObjectRelation( $contentObjectAttribute->attribute('data_int'),
                                                          $this->current_eZ_object->attribute('current_version'),
                                                          $this->current_eZ_object->attribute('id'),
                                                          $contentObjectAttribute->attribute('contentclassattribute_id')
                                                          );
            */
            $contentObjectAttribute->setAttribute( 'data_int', 0 );
            $contentObjectAttribute->store();
            
        }
        break;
        
        case 'ezobjectrelationlist':
        {
            // Remove any exisiting value first from ezobjectrelationlist
            
            $content = $contentObjectAttribute->content();
            $relationList =& $content['relation_list'];
            $newRelationList = array();
            for ( $i = 0; $i < count( $relationList ); ++$i )
            {
                $relationItem = $relationList[$i];
                eZObjectRelationListType::removeRelationObject( $contentObjectAttribute, $relationItem );
            }
            $content['relation_list'] =& $newRelationList;
            $contentObjectAttribute->setContent( $content );
            $contentObjectAttribute->store();
            
        }
        break;
    }
    
    // fromString returns false - even when it is successfull
    // create a bug report for that
    $contentObjectAttribute->fromString( $value );
    $contentObjectAttribute->store();
    
}

I hope this will help you.

Damien

Laurent Dorier

Monday 25 January 2010 1:54:38 am

Thanks a lot Damien,

working like a charm. :)

But finally... I do it without this extension using the eZContentObject class...
Thanks anyway for your very fast answer.

eZ debug

Timing: Jan 18 2025 00:01:22
Script start
Timing: Jan 18 2025 00:01:22
Module start 'content'
Timing: Jan 18 2025 00:01:23
Module end 'content'
Timing: Jan 18 2025 00:01:23
Script end

Main resources:

Total runtime0.7805 sec
Peak memory usage4,096.0000 KB
Database Queries206

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 587.7344180.8281
Module start 'content' 0.00540.6445 768.5625631.2969
Module end 'content' 0.64990.1305 1,399.8594341.0859
Script end 0.7804  1,740.9453 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.5141210.0002
Check MTime0.00160.1989210.0001
Mysql Total
Database connection0.00070.085110.0007
Mysqli_queries0.698189.44202060.0034
Looping result0.00230.29772040.0000
Template Total0.757397.020.3787
Template load0.00200.251120.0010
Template processing0.755496.779020.3777
Template load and register function0.00010.014110.0001
states
state_id_array0.00070.094010.0007
state_identifier_array0.00060.083320.0003
Override
Cache load0.00170.2208500.0000
Sytem overhead
Fetch class attribute can translate value0.00160.208340.0004
Fetch class attribute name0.00130.166480.0002
XML
Image XML parsing0.00150.198540.0004
class_abstraction
Instantiating content class attribute0.00000.0024100.0000
General
dbfile0.00280.3647330.0001
String conversion0.00000.000630.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
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
7content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 36
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs