Forums / Developer / Is it possible to add new attributes to an existing class programmatically?

Is it possible to add new attributes to an existing class programmatically?

Author Message

tom riddle

Monday 31 January 2011 8:09:57 am

Hi there,

I wonder if it's possible to add new attributes to an existing class with php code.

Would be nice to see an example snippet.

Thank you!

Tom

Thiago Campos Viana

Monday 31 January 2011 2:32:53 pm

Yes, we can!

Here's a sample about creating class and attributes using php.

Btw, welcome to ez community forum.

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

tom riddle

Monday 31 January 2011 11:28:41 pm

Great, thank you very much!

I'll try now... If there are further questions, I'll come back :)

Greetings
Tom

tom riddle

Tuesday 01 February 2011 4:52:20 am

"

I'll try now... If there are further questions, I'll come back :)

"

There I am, I was on the wrong way...

Adding new attributes to an existing eZContentClass is possible with the solution you explained before.

But I need to add new attributes programmatically to eZContentObject.

I'd like to try with
$new_attribute = eZContentObjectAttribute::create($contentclassAttributeID, $contentobjectID);
but where do I get "$contentclassAttributeID" from?

Greetings Tom

Thiago Campos Viana

Tuesday 01 February 2011 6:02:39 am

Here's another sample about handling content with php.

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

tom riddle

Tuesday 01 February 2011 6:24:30 am

Thank you!

But it seems only work for existing attributes? I need to create new attributes.

Any idea or short snippet?

I'm not sure where to get the 'contentclassAttributeID' from.

Thiago Campos Viana

Tuesday 01 February 2011 4:31:13 pm

You'll need to create class attributes first. You can't store objects attributes without defining these attributes as class attributes, at least with the default API.

But you can always create subnodes that could work as pseudo object attributes.

Getting the $contentObjectAttributeID:

$contentObjectAttributeID = $contentObjectAttribute->attribute('id');

Btw, you could read my tutorial:

A Quick and Friendly Introduction to eZPersistentObject

It would help you know how objects are stored in the database.

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

tom riddle

Wednesday 02 February 2011 2:04:19 am

Thank you, I've tested your advice now.

I have a class 'cars_article' and I want to create a new attribute 'keywords'.

I have done like this:

class MyTestingHandler extends eZContentObjectEditHandler
{
  function publish($contentObjectID, $contentObjectVersion)
  {
    $articleClass = eZContentClass::fetchByIdentifier('cars_article');
    
    if (is_object($articleClass))
    {
      $contentClassID = $articleClass->attribute('id');
      $classVersion = $articleClass->attribute('version');

      $DataTypeString = 'ezstring';
      $new_attribute = eZContentClassAttribute::create($contentClassID, $DataTypeString);
                
      $new_attribute->setAttribute('version', $classVersion);
      $new_attribute->setAttribute('name', 'Keywords');
      $new_attribute->setAttribute('is_required', 0);
      $new_attribute->setAttribute('is_searchable', 0);
      $new_attribute->setAttribute('can_translate', 0);
      $new_attribute->setAttribute('identifier','keywords');
                
      $dataType = $new_attribute->dataType();
      $dataType->initializeClassAttribute($new_attribute);
      $new_attribute->store();
    }

I fetched the object like this:

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

2 problems I got:

  1. In backend creating new article of class 'cars_article' I have now a new field 'keywords'. BUT if I want to add keywords to an existing article, I don't have the field. What's wrong here?
  2. How can I add keywords into the article programmatically?

I have a knot in my brain :D

Thanks & greetings
Tom

Thiago Campos Viana

Wednesday 02 February 2011 3:06:34 am

You need to create a new class version before start coding the new attribute, btw, you are not creating an ezkeywords attrib, you are creating an ezstring. I already posted a link on how to add content to an object programmatically: http://thiagocamposviana.blogspot.com/2010/08/ez-publish-44-attribute-helper.html

But I'm not sure what is the problem with the existing objects, maybe someone else could help here.

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br