New to ZF2. Pretty sure this is a very basic question as I can easily do it in procedural style, but finding the documentation difficult to work through. Any documentation links gladly received.
I am storing a form dropdown value as an integer, in the db. When I return the results to my view it is returning the integer using:
echo $this->escapeHtml($user->system);
How do I map this response so that I show the actual value of the dropdown that the user selected in the form?
One option would be via a view helper, although there are other ways.
Create an interface for any entity that is 'aware' of the system, say
SystemAwareInterface
. Ensure your user class (or any other class) implements this interface and returns the system id.Create a view helper, i'm assuming a top level namespace of
System
and that you have some kind of service that can load the record from the database by it's identity (lets call itSystemService
with a methodloadById()
).Then register the service with the
ViewHelperPluginManager
by adding a factory togetViewHelperConfig
.Now in the view script you can echo the system name using the helper.
You can also use other view helpers within you new helper; so you could get the
escapeHtml
helper and escape HTML content within thegetName()
method (I'll leave that to you).