I am trying to implement some simple business logic in my Entity to pass it to Twig but I seem to fail somewhere.
I have two entities Users and Customers who are extending an abstract entity called Person and share some field including Gender.
I want to create an isser in the Person Entity that gets the integer value of the gender(0 or 1), and converts it to a string of Male or Female like:
public function isMale()
{
if($this->gender == 0) {
$myGender = "Male";
}
else {
$myGender = "Female";
}
return $myGender;
}
so that I can pass this business logic in my Twig template something like:
{% for person in person %}
<tr>
<td>{{ person.gender.isMale }}</td>
<tr>
{% endfor %}
The person attribute in Twig is a query passed from the Controller that holds some results in an array, which are the joined fields of both Entities.
I have created a Twig AppExtension as a filter, which works, and I simply want to pass this to the Entity logic but all I get is: Impossible to access an attribute ("isMale") on a integer variable ("0").
Probably Virtual Property??
You are trying to do something like this:
person->gender() = 0;
Then:
gender->isMale();
But, you can see that 0->isMale()
is invalid.
So you need something like:
{{ person.getGender }}
Where getGender
is a getter that returns a string representation of the gender (example "male").
In your twig template, you are treating gender
as an object that has the method isMale
. If gender was an entity/object itself, then what you have of person.gender.isMale
would be valid. You should be able to have a working solution if you just change it to person.isMale
{% for person in person %}
<tr>
<td>{{ person.isMale }}</td>
<tr>
{% endfor %}