Forums / Developer / Adding attribute (with function) to existing class
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 Exponential 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,
7x | https://se7enx.com/ Brookins Consulting | https://brookinsconsulting.com/ Certified | http://web.archive.org/web/201012...th.ez.no/certification/verify/380350 Solutions | https://projects.exponential.earth/users/community/brookins-consulting eZpedia community documentation project | http://ezpedia.se7enx.com
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
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.
Monday 18 October 2010 10:56:04 am
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