Forums / Developer / How to update an attribute of an object with PHP?

How to update an attribute of an object with PHP?

Author Message

Felix Laate

Friday 10 March 2006 5:18:50 am

Hi all,

just a quick question to kick off the weekend!

I have added an attribute "active" to the user class. This is not set by default.

Now I need to set it programmicaly from an externsion. I don't know the nodeid or the objectid, but i know the phonenumber (which also was added to the user class as an attribute).

So I gather I need to fetch the user with the given phonenumber and then update the active-attribute of that user.

Any help might make my weekend pleasant!

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Kåre Køhler Høvik

Sunday 12 March 2006 12:07:00 am

Hi

This depends a bit on what kind of attribute it is, but I guess it's a boolean/checkbox in your case. Here is how to do it:

$object = eZContentObject::fetch( <object_id> );
$dataMap = $object->attribute( 'data_map' );
$active = $dataMap['<class attribute identifier>']; // Guess 'active' is your case
$active->setAttribute( 'data_int', <0|1> );
$active->sync();

Kåre Høvik

Felix Laate

Monday 13 March 2006 3:29:29 am

Hi Kåre,

thank you very much for your answer. It helped me alot!

Then I only need to figure out how I can get the objectid of the user with the given phonenumber.

Any ideas?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Kåre Køhler Høvik

Monday 13 March 2006 4:45:14 am

Hi Felix

You can use the subtree function in content object tree node. Example :

$node = eZContentObjectTreeNode::subtree( 'AttributeFilter' => 
        array( array( 'user/number', '=', '12345678' ) ) ), <parent node id> );
$object = $node->attribute( 'object' );

See also : http://ez.no/doc/ez_publish/technical_manual/3_6/reference/modules/content/fetch_functions/list

Kåre Høvik

Felix Laate

Monday 13 March 2006 5:31:38 am

Hi again,

thank you again!

The code returned a

unexpected T_DOUBLE_ARROW

I think I solved it by changing the code to

$node = eZContentObjectTreeNode::subtree( array('AttributeFilter' => array( array(  'user/mobiltelefon', '=', _a_real_phone_number_))), 125 );  	

But now the problem is that it ruturns nothing. I also tried to change the attribute and fetch by the login-name, but it gave the same result.

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Kåre Køhler Høvik

Monday 13 March 2006 5:48:46 am

Hi Felix

125 is a parent node id, not a parent object id ?
If you enable SQL output, how does the SQL look like ?
What kind of datatype is the "mobilnummer" ?

Kåre Høvik

Felix Laate

Monday 13 March 2006 5:57:22 am

Hi Kåre,

125 is the NODE id where the users I want to fetch amongst are.

Here is the SQL output:

SELECT ezcontentobject.*,
                       ezcontentobject_tree.*,
                       ezcontentclass.name as class_name,
                       ezcontentclass.identifier as class_identifier
                       
                       , ezcontentobject_name.name as name,  ezcontentobject_name.real_translation 
                   FROM
                      ezcontentobject_tree,
                      ezcontentobject,ezcontentclass
                      , ezcontentobject_name 
                      
                      , ezcontentobject_attribute a0 
                      
                   WHERE
                       path_string like '/1/5/125/%' and  
                      
                      
                      
                                       a0.contentobject_id = ezcontentobject.id AND
                                       a0.contentclassattribute_id = 238 AND
                                       a0.version = ezcontentobject_name.content_version AND
                                       a0.language_code = ezcontentobject_name.real_translation AND 
                            ( a0.sort_key_string = 12345678) AND 
                      ezcontentclass.version=0 AND
                      node_id != 125 AND
                      ezcontentobject_tree.contentobject_id = ezcontentobject.id  AND
                      ezcontentclass.id = ezcontentobject.contentclass_id AND
                      
                      
                       ezcontentobject_tree.contentobject_id = ezcontentobject_name.contentobject_id and
                                   ezcontentobject_tree.contentobject_version = ezcontentobject_name.content_version and
                                   ezcontentobject_name.content_translation = 'nor-NO' 
                      AND ezcontentobject_tree.is_invisible = 0
                       AND ((ezcontentobject.section_id in (1)) OR (ezcontentobject.section_id in (3))) 
                 ORDER BY  path_string ASC

"mobiltelefon" is text line.

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Felix Laate

Wednesday 15 March 2006 3:16:05 am

Hi again,

just wanted to tell you that I figured out what the problem was.

The object(s) I wanted to fetch was situated in the users section, and I guess the <i>eZContentObjectTreeNode</i> class is the wrong class to use to fetch something from there.

Had a look at the <i>eZUser</i> class, but couldn't find anything to meet my needs. So I moved the objects into the content structure.

Now it works, but I'm not completly satisfied as the objects really belong to the user section.

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Xavier Dutoit

Tuesday 03 April 2007 6:04:51 am

Hi,

Access right issue ? What's the user that run the php code ?

http://www.sydesy.com

André R.

Tuesday 03 April 2007 6:05:25 am

Example for selection datatype, updating selection index from 3 to 4:

$dataMap = $object->attribute( 'data_map' );
$active = $dataMap['status'];

if ($active->attribute( 'data_text' ) == '3')
{
    $active->setAttribute( 'data_text', '4' ); 
    $active->sync();
}

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

eZ debug

Timing: Jan 31 2025 06:25:31
Script start
Timing: Jan 31 2025 06:25:31
Module start 'content'
Timing: Jan 31 2025 06:25:31
Module end 'content'
Timing: Jan 31 2025 06:25:31
Script end

Main resources:

Total runtime0.4928 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0112 588.0625180.8125
Module start 'content' 0.01120.0094 768.8750110.0547
Module end 'content' 0.02070.4720 878.9297538.3750
Script end 0.4927  1,417.3047 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00531.0663200.0003
Check MTime0.00150.3132200.0001
Mysql Total
Database connection0.00120.235810.0012
Mysqli_queries0.380377.17951410.0027
Looping result0.00180.36821390.0000
Template Total0.471495.710.4714
Template load0.00100.205410.0010
Template processing0.470495.465210.4704
Override
Cache load0.00070.138010.0007
Sytem overhead
Fetch class attribute can translate value0.00080.162610.0008
XML
Image XML parsing0.00020.038910.0002
General
dbfile0.051410.4359200.0026
String conversion0.00000.001030.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs