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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 05:20:37
Script start
Timing: Jan 18 2025 05:20:37
Module start 'layout'
Timing: Jan 18 2025 05:20:37
Module start 'content'
Timing: Jan 18 2025 05:20:37
Module end 'content'
Timing: Jan 18 2025 05:20:37
Script end

Main resources:

Total runtime0.0326 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0078 588.0469152.6406
Module start 'layout' 0.00780.0043 740.687539.4844
Module start 'content' 0.01220.0182 780.171997.5078
Module end 'content' 0.03030.0023 877.679738.2891
Script end 0.0326  915.9688 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00298.8440140.0002
Check MTime0.00113.4236140.0001
Mysql Total
Database connection0.00123.653010.0012
Mysqli_queries0.003811.733530.0013
Looping result0.00000.064310.0000
Template Total0.00175.410.0017
Template load0.00102.972110.0010
Template processing0.00082.368610.0008
Override
Cache load0.00072.126110.0007
General
dbfile0.012538.338580.0016
String conversion0.00000.036540.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs