Forums / Developer / Adding attribute (with function) to existing class

Adding attribute (with function) to existing class

Author Message

Antoine Dubois

Monday 18 October 2010 2:41:59 am

Hi,

Consider a class which definitions are in the DB (created with the admin interface)...

How could I extend this class in PHP to add some derived attribute (attributes filled on the fly when fetch is operated)?

Example:

Class : "door" with some attributes that was created in admin

I'd like to add an attribute "open" but the value of this attribute would depend on extra data (like domotic stuff) and the value of other "door" class attributes

Thank you in advance for your help...

Heath

Monday 18 October 2010 4:23:40 am

Hello Antoine,

Welcome to the eZ Publish Community!

I do not think what you describe is very possible or realistic.

I think you would get better results creating a custom datatype.

http://share.ez.no/learn/ez-publish/creating-datatypes-in-ez-publish-4/

Cheers,

Heath

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Antoine Dubois

Monday 18 October 2010 6:11:01 am

Hi Heath,

Thanks for your answer even if it it tells me I'm in the wrong direction... ;-)

I will try to use the datatypes but I don't think it will fit as what I ask is a derived attribute and so depends on other attributes of the class... It's class-level and not attribute-level like datatype...

I know it's possible to code custom classes but I don't know if those classes can be used as Content Classes (with all the features it provides)...

Do you think of another way to realize this?

Thanks again

Antoine

Heath

Monday 18 October 2010 7:28:34 am

Well Antoine,

Datatypes can be affected by class level properties. It just depends on how the datatype is programmed. Take the ezselection datatype which stores the available options for the selection list at a class level yet presents these options at an content object attribute level. http://pubsvn.ez.no/doxygen/4.4/html/classeZSelectionType.html

I do not see another way to realize this outside a custom datatype.

Cheers,

Heath

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Antoine Dubois

Monday 18 October 2010 10:56:04 am

Hi Heath,

That's a lead I'm following...

For now I'm trying to create a datatype (only usefull on my class) that define only the method "objectAttributeContent" that receive the attribute object as parameter. From that I can get infos on it. But it fits only the class that contains this kind of datatype and that could be a problem in the future

class  MyType extends eZDataType{
      /**
      * @param eZContentObjectAttribute $contentObjectAttribute
      */
      public function objectAttributeContent($contentObjectAttribute){
          
          /**
          * @var eZContentObject
          */
          $object = $contentObjectAttribute->object();
          
          if(!$object->ClassIdentifier == 'myclass'){
              return;
          }
          
          return setValueWithbusinessLogic($object);
      }
      
      function setValueWithbusinessLogic($object){
          //Mock of what could be done
          $value = 100 + $object->attribute('otherAttribute');
          //Add DB, ezIni,... accesses
          return $value;
      }
  }

Am I right in the method?

It's something I could need to do this frequently. Could we think to extend (or hack) the actual ezContentObject class to implement a nice way to read INI files in combinaison with a generix datatype or extension method?

In fact, a nice way to create custom derived attribute to avoid tight coupling between the class definition and the attribute!

Anyway, thanks for the help ;-)

Cheers

Antoine

eZ debug

Timing: Jan 18 2025 02:12:48
Script start
Timing: Jan 18 2025 02:12:48
Module start 'content'
Timing: Jan 18 2025 02:12:48
Module end 'content'
Timing: Jan 18 2025 02:12:49
Script end

Main resources:

Total runtime0.9740 sec
Peak memory usage4,096.0000 KB
Database Queries201

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0078 587.8438180.8203
Module start 'content' 0.00780.7972 768.6641566.0859
Module end 'content' 0.80500.1689 1,334.7500337.3672
Script end 0.9739  1,672.1172 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00410.4178210.0002
Check MTime0.00150.1515210.0001
Mysql Total
Database connection0.00150.153410.0015
Mysqli_queries0.885790.93232010.0044
Looping result0.00240.24691990.0000
Template Total0.946197.120.4730
Template load0.00160.168820.0008
Template processing0.944496.967520.4722
Template load and register function0.00020.025410.0002
states
state_id_array0.00160.163310.0016
state_identifier_array0.00080.077020.0004
Override
Cache load0.00140.1425460.0000
Sytem overhead
Fetch class attribute can translate value0.00200.208930.0007
Fetch class attribute name0.00110.112660.0002
XML
Image XML parsing0.00110.110630.0004
class_abstraction
Instantiating content class attribute0.00000.001670.0000
General
dbfile0.01361.3926280.0005
String conversion0.00000.000530.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
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
7content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 18
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs