This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Developer » Can $attribute->store() be replaced...
expandshrink

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

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

Monday 06 June 2005 3:17:40 pm - 6 replies

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 !

Monday 06 June 2005 4:41:03 pm

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

happy.gif Emoticon

Monday 06 June 2005 7:01:08 pm

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.

Modified on Monday 06 June 2005 7:04:10 pm by Eirik Alfstad Johansen

Thursday 09 June 2005 12:18:11 pm

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

Thanks in advance !

Thursday 09 June 2005 4:33:26 pm

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

Thursday 09 June 2005 7:04:18 pm

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 !

Modified on Thursday 09 June 2005 7:10:50 pm by Eirik Alfstad Johansen

Friday 10 June 2005 8:34:09 am

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

Modified on Friday 10 June 2005 8:34:50 am by Tom Couwberghs

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from