New module extension

Author Message

David Santiso

Wednesday 04 May 2011 2:28:28 pm

Hi,

I'm developing an extension that updates an attribute of an object. I read the tutorial and explains how to insert and query data from the database, but doesn't explain how to update.

I've created the module, function and class, but I'm blocked. In ezpersistentobject class ther is a function updateObjectList, but I have to pass a variable $parameters. My code is this:

exempleclass.php

class ExempleClass extends eZPersistentObject {
    
    function exempleclass( $row ) {
        
        $this->eZPersistenObject( $row );        
    }
    
    function definition() {
        
        return array( 'fields' => array( 'id' => array( 'name' => 'id', 
                                                        'datatype' => 'integer', 
                                                        'default' => 0, 
                                                        'required' => true ),
 
                                         'user_id' => array( 'name' => 'version', 
                                                             'datatype' => 'integer', 
                                                             'default' => 0, 
                                                             'required' => true ),
 
                                         'created' => array( 'name' => 'data_int', 
                                                             'datatype' => 'integer', 
                                                             'default' => 0, 
                                                             'required' => false ) ),  
                      'class_name' => 'exempleclass', 
                      'name' => 'ezcontentobject_attribute' ); 
    }
    
    function update( $attribute_id, $version, $value ) {
        
        $row = array( 'definition' => array( 'name' => 'ezcontentobject_attribute',
                                             'fields' => array( ),
                                             'key' ),
                      'update_fields' => array( 'data_int' => $value ),
                      'conditions' => array( 'id' => $attribute_id,
                                             'version' => $version ) );
        
        return new exempleclass( $row );
    }
}

exemplefunction.php

include_once 'extension/example/classes/exempleclass.php';

$Module = $Params['Module'];

$attributeId= $Params['AttributeId']; 
$version = $Params['Version'];

$ExampleClass= ExampleClass::update( $attributeId, $version, 23 );

How do I call the function to update?

Thanks,

David

Marko Žmak

Thursday 05 May 2011 12:28:42 am

For such a simple task as changing one attribute of an object, you don't need to extend the eZPersistentObject class. What you need is basically this:

  • get both object ID and attribute ID as a module parameter
  • fetch the object using the static fetch function of eZContentObject class
  • change the attribute and store it
  • sroe or publish the object

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

David Santiso

Thursday 05 May 2011 1:51:40 am

Ok, but if I call the store function I'll be making a new INSERT. I want to do an UPDATE.

Marko Žmak

Thursday 05 May 2011 2:20:41 am

"

Ok, but if I call the store function I'll be making a new INSERT. I want to do an UPDATE.

"

No it won't. The store method does an INSERT only if you create a new object of class eZContentObject. And since you will be fethcing an existing object it will do an UPDATE in your case.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

David Santiso

Thursday 05 May 2011 2:36:01 am

Ah!

Ok, thanks. I didn't understand it.

David Santiso

Thursday 05 May 2011 6:09:58 am

Ok. I've to do this in function.php:

includes...
 
$Module = $Params['Module'];

$objectId = $Params['ObjectId'];
$attributeId = $Params['AttributeId']; 
$version = $Params['Version'];
 
$contentObject= eZContentObject::fetchAttributesByIdentifier( $attributeId, $version, false, true );

... ?

But, how do I change the attribute?

Marko Žmak

Thursday 05 May 2011 6:32:27 am

You can find some usefull tips about it here:

  • http://share.ez.no/forums/developer/can-attribute-store-be-replaced-by-object-store
  • http://share.ez.no/forums/developer/how-to-edit-store-user-attributes-within-a-extension

The last comment in the first thread is the simplest, but I think it won't clear the cache.

As for the detailed docs about it I don't think there is any, you'll have to dig and try a little bit until you figure out what's the best for you.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

David Santiso

Thursday 05 May 2011 11:57:28 am

Thank you very much.

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