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

eZ debug

Timing: Jan 18 2025 03:02:44
Script start
Timing: Jan 18 2025 03:02:44
Module start 'content'
Timing: Jan 18 2025 03:02:45
Module end 'content'
Timing: Jan 18 2025 03:02:45
Script end

Main resources:

Total runtime1.0215 sec
Peak memory usage4,096.0000 KB
Database Queries216

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 589.2109180.7891
Module start 'content' 0.00540.8989 770.0000633.0938
Module end 'content' 0.90430.1171 1,403.0938345.0234
Script end 1.0215  1,748.1172 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00370.3601210.0002
Check MTime0.00140.1396210.0001
Mysql Total
Database connection0.00070.071510.0007
Mysqli_queries0.939291.93472160.0043
Looping result0.00200.19592140.0000
Template Total0.997997.720.4989
Template load0.00200.195320.0010
Template processing0.995997.485620.4979
Template load and register function0.00010.010410.0001
states
state_id_array0.00080.077910.0008
state_identifier_array0.00120.114020.0006
Override
Cache load0.00180.1740670.0000
Sytem overhead
Fetch class attribute can translate value0.00120.120730.0004
Fetch class attribute name0.00100.0936100.0001
XML
Image XML parsing0.00070.072830.0002
class_abstraction
Instantiating content class attribute0.00000.0037130.0000
General
dbfile0.00330.3223270.0001
String conversion0.00000.000430.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
9content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
19content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/quote.tpldatatype/ezxmltext/quote.tplextension/ezwebin/design/ezwebin/override/templates/datatype/ezxmltext/quote.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/header.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/header.tplEdit templateOverride template
1content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
1content/datatype/view/ezxmltags/ol.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ol.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 48
 Number of unique templates used: 12

Time used to render debug report: 0.0001 secs