Author Topic

Pål J Didriksen

Saturday 09 July 2005 5:26:38 am

Comment19347

I have used the following code to change an attribute value and store the change. Found most of the code somewhere on ez.no, so you should probaly thank someone else ;)

        // Fetch current version of the object (provided you have the objectID)
        $contentObject =& eZContentObject::fetch( $objectID );
        $contentObjectVersion =& $contentObject->version( $contentObject->attribute( 'current_version' ) );
        $contentObjectAttributes =& $contentObjectVersion->contentObjectAttributes();

        // Loop through the attributes, to find the right one (in this example, contentclassattribute_id=232)
        foreach (array_keys($contentObjectAttributes) as $key)
        {
            $contentObjectAttribute =& $contentObjectAttributes[$key];
            $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();

           if ($contentClassAttribute->attribute("id") == 232)
           {
               // Found it!
               $oldVal  = $contentObjectAttribute->attribute("data_int");
               $newVal  = $oldVal + 1;

               // Insert new value and store it
                                $contentObjectAttribute->setAttribute("data_int", $newVal);
               $contentObjectAttribute->store();
           }
        }