Forums / Developer / Can $attribute->store() be replaced by $object->store()

Can $attribute->store() be replaced by $object->store()

Author Message

Eirik Alfstad Johansen

Monday 06 June 2005 6:17:40 am

Hi,

I've written an a script which imports and updates objects, but it runs really slow so I'm taking a look at what I can do to optimize it.

Browsing my code, I noticed that I do the following for each attribute:

$objectAttributes[$i]->setAttribute( 'data_text', $data );
$objectAttributes[$i]->store();

And then the following at the end:

$object->store();

So, my question is whether or not I need to call the store method for each attribute, or whether the calling it for the object at the end is sufficient.

Thanks in advance !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Kristian Hole

Monday 06 June 2005 7:41:03 am

The short answer to your post: yes.

The long answer: The attributes and objects are stored in different tables in the database, so you need to store them individually

:)

Kristian

http://ez.no/ez_publish/documenta...tricks/show_which_templates_are_used
http://ez.no/doc/ez_publish/techn...te_operators/miscellaneous/attribute

Eirik Alfstad Johansen

Monday 06 June 2005 10:01:08 am

Hi Kristian,

Thanks for the heads up. I was thinking that maybe the object method looped through the attributes and stored them as well, but I guess that's not the case.

I must say, I find it confusing to create, or in this case modify, ezp objects in PHP, making me think that I might be using uneccessary code. Here's a short hand version of what I do. Are there any commands that could be dropped?

// fetch object
$object = eZContentObject::fetch($objectID);

// not sure if this fetches the current version or creates a new version
$version =& $object->version( $object->attribute('current_version') );

// set version status to draft
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

// update object name
$object->setAttribute( 'name', $name);

// fetch object attributes
$objectAttributes =& $version->contentObjectAttributes();

// update and store attribute
$objectAttributes[0]->setAttribute( 'data_int', $data_int );
$objectAttributes[0]->store();

// store object
$object->store();

// publish the new/current/whatever version
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $object->attribute( 'id' ), 'version' => $object->attribute('current_version') ) );

Running this code takes about 0.2 seconds (I update a few more attributes) and when the objects are in the hundreds, the execution time really starts to drag out.

I would have no problem modifying the existing current version instead of creating a new one if it would save me some time.

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Eirik Alfstad Johansen

Thursday 09 June 2005 3:18:11 am

Please, guys? I'm assuming this would be easy to answer for an eZP employee.

Thanks in advance !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Tom Couwberghs

Thursday 09 June 2005 7:33:26 am

Hi Erik,

you can just change the current version (if this is what your looking for) and forget about republishing the object.

It is the publish trigger that takes so long, because it sends the object trough a range of actions (checking workflow, re-indexing, ...)

hope this helps

Tom

Eirik Alfstad Johansen

Thursday 09 June 2005 10:04:18 am

Hi Tom,

Thanks a lot for helping me out. Here's what I tried following your suggestion.

1. Removed the publish code: Object content is not updated unless I enter edit mode and publish it from there. Also complains about unpublished draft.

2. Removed the publish code and code which I (assume) creates a new draft (at the top): Same as above, but no complaining about unpublished draft.

3. Kept publishing code, and removed the creation of a new version: This seems to work, but it isn't saving me a lot of time (a measly 0.02 seconds pr entry).

Am I missing something here, or was this your suggestion?

Thanks in advance !

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Tom Couwberghs

Thursday 09 June 2005 11:34:09 pm

Hi Erik,

just do the following:

$object = eZContentObject::fetch($objectID);

// update object name
$object->setAttribute( 'name', $name);
$object->store();

// fetch object attributes
$objectData = &$object->dataMap();

$attr = &$objectData['name_of_your_attrib'];

$attr->setAttribute( 'data_int', 5 );
$attr->store();

I haven't tested this code, so beware of typo's.

--T