On Wed, Aug 14, 2013 at 8:39 AM, András Csányi <[email protected]> wrote: > The concern I have is related rather programming style than the > framework itself. On the other hand, I'm not a programmer, I'm just > spending my time writing programs in different languages. > > I want to display a menubar where a menu belongs to a category. I have > a menu table and a category table, both has its Model. So, in the > controller I fetch the data from both table and I have to merge them > into an array. I can do it in the controller, in the view .phtml file > and in an external library like class. > > So, what is the proper solution if I follow the design patterns thinking? > - manipulating data in the controller class which can cause big, huge > methods and on the other hand, reusing the code is not possible > - manipulating in the view file. To be honest, I feel it very > uncomfortable because the data should be manipulated somewhere else, > manipulating is not part of the displaying > - manipulating in a library class, where the logic clearly separated > from the controller and the view and possible to reuse easily.
As you're already discovering, there's more than one way to do these things. The problem is keeping a clean separation of concerns. I personally would do this somewhere in the model layer, but not necessarily directly in the various entity/value objects. Instead, create a "view model" for representing the information, pass your model(s) to it, and have this class extract the information it needs: $menuData = new MenuViewModel($menuTable, $categoryTable); Inside, it might grab the data and merge it, remove keys, create composite keys, etc. It doesn't have to extend Zend\View\Model\ViewModel, either -- this is simply a way to create a translation layer between the model layer and how you want to represent it. This removes the logic from the controller, as well as the view, and helps you describe in programmatic terms the relations between the various data sources and the representation of them. -- Matthew Weier O'Phinney Project Lead | [email protected] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected]
