Book a Demo

Author Topic: Getters and Setters are blank in generated php  (Read 3132 times)

cbrandenburg

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Getters and Setters are blank in generated php
« on: June 27, 2006, 07:20:15 am »
I'm new to EA and have a very limited time for the evaluation so if this is an obvious setting please forgive my ignorance.

I've created a class to encapsulate the $_SERVER variables in php.  So for example $_SERVER['HTTP_HOST'] will map to the hostName attribute in the object.

I created the diagram and added the attributes, I selected the Property checkbox to create the getters and setters as well.

When I go to generate the code, the getters and setters are empty:

[size=10]
/**
* The hostName field is passed in on the request in the $_SERVER['HTTP_HOST']
* variable. This value is the hostname to which the request was sent.  
*/
function gethostName()
{
}
[/size]


I would have hoped for the following:
[size=10]
/**
* The hostName field is passed in on the request in the $_SERVER['HTTP_HOST']
* variable. This value is the hostname to which the request was sent.  
*/

function gethostName()
{
   return hostName;
}
[/size]


Any assistance would be most appreciated.

thomaskilian

  • Guest
Re: Getters and Setters are blank in generated php
« Reply #1 on: June 27, 2006, 12:48:25 pm »
You need to modify the Code Gen macros for PHP.

Open the Operation Body. Insert something like

%if (opStereotype == "get property")%
return %opName%;
%endIf%

You need to trick around with substrings, but that should be not too difficult to solve.

mikewhit

  • EA User
  • **
  • Posts: 608
  • Karma: +0/-0
  • Accessing ....
    • View Profile
Re: Getters and Setters are blank in generated php
« Reply #2 on: June 28, 2006, 03:25:31 am »
If this is not already in the documentation (or only very indirectly), then it should be considered for explicit inclusion.

Sounds like a very basic FAQ.

cbrandenburg

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Getters and Setters are blank in generated php
« Reply #3 on: June 28, 2006, 04:27:08 am »
Thanks for the information.  I also searched for "property get" in the forum and found that I needed to create two  new stereotypes.  So far I've struck out getting it to run the new code but I'll tinker some more when I have some time.

For some reason the first shot printed out:

%if (opStereotype == "get property")%
return hostName;

so it got closer.

I'm not sure if it was in the docs or not but I did search the help system for what I thought I needed and came up empty.

Thanks again,
Chris

thomaskilian

  • Guest
Re: Getters and Setters are blank in generated php
« Reply #4 on: June 28, 2006, 12:37:20 pm »
Try something like  
return %MID (opName, 4, 99)%
to cut off the leading get/set.

Use "function macros" to search the help.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Getters and Setters are blank in generated php
« Reply #5 on: June 28, 2006, 03:15:22 pm »
Ok, I'm not sure why that isn't in the templates already, but the following template should do the job.

Code: [Select]
%if genOptPHPVersion == "5.0" and elemType=="Interface"%
;
%endTemplate%

{
$wrap = %genOptWrapComment=="-1" ? "-1" : "40"%
$behaviour = %WRAP_LINES(opBehaviour, $wrap, "\t//", "")%
%if $behaviour != ""%
\n$behaviour\n\n
%endIf%
%if opCode != ""%
%WRAP_LINES(opCode, "-1", "\t", "")%
%elseIf opStereotype == "property get" and opTag:"attribute_name" != ""%
\treturn %dl%this->%opTag:"attribute_name"%;
%elseIf opStereotype == "property set" and opTag:"attribute_name" != ""%
\t%dl%this->%opTag:"attribute_name"% = %dl%newVal;
%endIf%
}

I'll point out two things from there.
1. There are no parenthesis in the syntax for the if condition.  (See http://www.sparxsystems.com/EAUserGuide/index.html?controlmacros.htm)
2. Property methods created with the property checkbox get a tagged value "attribute_name" that contains the name of the attribute they are a property for.  The stereotypes and tagged values used for PHP code engineering are documented at http://www.sparxsystems.com/EAUserGuide/index.html?php_conventions.htm