Forums / Setup & design / Simple Content Object Edit

Simple Content Object Edit

Author Message

Daniel Guerrier

Saturday 06 August 2005 8:55:19 pm

I need to update a content object attribute on a regular basis. All the edit example I see in the docs show a form post. All I want to to is fetch an array of content objects, update an attribute and commit the change. Is there any doc that shows how to do this with just template code, and no form posts?

Daniel Guerrier

Sunday 07 August 2005 9:00:02 am

The question is, can this be done using standard ez template operators, or do I have to customize something. Again I just want to get all instances of a certain content class and update one of the attributes. If it requires customization can a starting point be given.

Thanks.

Bertrand Dunogier

Sunday 07 August 2005 11:27:04 am

Yes, it requires customization. You can create a shell script invoked by runcronjobs, and update your content from there using the eZ Publish Framework.

Daniel Guerrier

Sunday 07 August 2005 3:05:10 pm

Thanks,

That's what I had in mind.
Is there an example that I can look at to get a start on how to put this together?

Xavier Dutoit

Monday 08 August 2005 12:11:57 am

Hi,

Have a look at the contribs, you're going to find inspirations.

For instance the import contribs show you how you modify attributes.

X+

P.S. I'd like to see your result as a contrib, looks like you're not the only one.

http://www.sydesy.com

Daniel Guerrier

Wednesday 10 August 2005 10:27:45 am

I've looked for information on this is many places, including that book which really needs some updating.

Can someone please give me a clear example on how to use the ez framework to fetch the instances of a certain content class and edit the attributes?

I tried the following but nothing is returned. Again there is no clear concise example or explanation on using ez for backend processing of content. Everything deals with retrieve data via templates and template code.

$contentClassIdentifier = 'mycontentclass';
$class =& eZContentClass::fetchByIdentifier( $contentClassIdentifier );
$contentClassID = $class->attribute( 'id' );

I am getting an error that $class is not an object so I am assuming that nothing is getting returned.

Thank you very much.

Daniel Guerrier

Wednesday 10 August 2005 6:21:31 pm

I'm sure I am doing something wrong but I have know idea what is.

Can someone provide an overview of how to use the framework and also where to place the files on the file system? Right now I have the above code placed in a .php file in my ezroot dir.

Marko Žmak

Thursday 11 August 2005 1:32:56 am

You could make a PHP script and use the functions and classes in eZ library that comes with eZ installation. Unfortunately, that's all the info I can get you, I havent tried this yet. But I've seen people do it.

Try searching the contribs for more examples.

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

Hubert Farnsworth

Daniel Guerrier

Thursday 11 August 2005 6:56:20 am

Could one of the folks that have been seen doing this shed some light on this? I can't be the only person that would like to do something like this.

I'm sure this will help many people if an explanation was given.

I went to the contribs and all deal with importing and creating objects. I want to retrieve existing objects edit and save. In addition, I tried creating a stand alone php script as shown earlier but I am not getting anything returned.

All I need is a starting point on how and were to create the php script and how to make use of the framework.

Please help!!

Hans Melis

Thursday 11 August 2005 7:48:45 am

I'm a little confused as to what you really want to do: edit attributes of a bunch of objects or edit attributes of a contentclass. I'll assume you want to change an attribute on a bunch of objects.

First of all, you need to determine if you want all objects of a contentclass, or just a selection of them . If you want all objects, use this:

$objects =& eZContentObject::fetchSameClassList( $contentClassID );

If you prefer to work with the identifier of a contentclass instead of passing the ID directly into the above function, do this:

$class =& eZContentClass::fetchByIdentifier( $identifier );
if( is_object( $class ) ) // This is a safety check
{
  $objects =& eZContentObject::fetchSameClassList( $class->attribute( 'id' );
  ...
}

If you want a selection of the objects, you can use the PHP equivalent of the template fetch('content','tree',...) call.

$nodes =& eZContentObjectTreeNode::subTree( $params, $nodeID );

For the params, I advise you to take a look at the first lines of that function to see their names (they're similar to the template names). $nodeID is what you'd call parent_node_id in the template version.
NOTE: The subTree() function will return objects of the eZContentObjectTreeNode class. To get the contentobject, loop over the returned nodes and get the attribute 'object'.

Now let's go to the next level and assume you're somewhere in your code where you're down to working with a single contentobject at a time.

Again, there are two ways to access the attributes of a contentobject: by numerical id or by identifier (identifier = identifier of the contentclass attribute). By identifier is the best way.

$objectData =& $object->dataMap();

That will get the data map of an object: an associative array with the identifier of an attribute as key, and the contentobject attribute as value. Let's say objects of that class have a text line attribute with identifier 'title'.

// $attribute will be set to an object of the class eZContentObjectAttribute
$attribute =& $objectData['title'];

To actually change the content of that attribute, you'll have to follow some steps that are different for each datatype. You'll have to look at the code of the datatype that you want to change and adapt the example I'll give for text line aka ezstring.

$attribute->setAttribute( 'data_text', 'Hello World!' );
$attribute->store();

Don't forget the store()! This example is the most basic way of changing the value of an attribute.

That should get you going... I hope.

Hans
http://blog.hansmelis.be

Daniel Guerrier

Thursday 11 August 2005 7:59:43 am

Thank you Hans

But I tried starting this and started small with

$contentClassIdentifier = 'mycontentclass';
$class =& eZContentClass::fetchByIdentifier( $contentClassIdentifier );

However I am not getting a valid object back from fetch call. Am I supposed to place the file in a specific place and do I have to do some sort registration in some ini to get this to work.

I want to fetch all instances of a specific content class and edit on attribute.

is_object( $class ) always fails for me.

Hans Melis

Thursday 11 August 2005 8:07:00 am

Where is your file now? Is it in an extension?

The easiest way would be to drop it in the cronjobs/ folder and add this to settings/override/cronjob.ini.append(.php):

[CronjobPart-editobjects]
Scripts[]
Scripts[]=yourphpfile.php

Then open a console and make sure you're in the root of your eZ publish install.
In linux, type:

./runcronjobs.php editobjects

In windows:

C:\php\cli\php.exe -C runcronjobs.php editobjects

Replace the path to PHP with your path and make sure to use the CLI binary and not the CGI php.exe.

For the script itself, make sure that the identifiers you use actually exist in your eZ publish site.

Hans
http://blog.hansmelis.be

Daniel Guerrier

Thursday 11 August 2005 8:15:09 am

Ok - I'll give it a try.

Right now I just placed it in the ezroot directory and ran it thru the browser.

Tian DK

Wednesday 17 August 2005 4:48:02 am

Hi Daniel

What progress have you made? I might have to look into this issue soon as well, just have not had the time yet. Will be interesting to know whether you have been able to crack this one.