Zend2 - how to get access to model not in controller

my zend2 application have some functions moved to other sources, for example my controller looks like this:

public function someAction() {
  $logic = new \Home\Logic\Home($this->getServiceLocator);

  return ('data' => $logic->getSomeStuff());

I like this way of building my application, cuz I was used to do it this way in Zend1, which makes my code pretty simple and easy to read/edit. Whole logic wasn't part of controller code. In Zend2 I have many problems with that - first thing - I have to push serviceLocator in my logic constructor - which is very bad solution for me. :( Sometimes I need to have access to model in view - for many reasons (for example to get easy access to dictionary fields - that means in table owners I have id_car, which links to table cars. I don't want to make view for that but just point in my model owners which field should be used in view to read from another model... Let's take a look at my Logic now:

public function getSomeStuff() {
  $model = $this->serviceLocator->get('someTable');

  return $model->fetchAll();

My question is - how to get access to my models without pushing serviceLocator from controller to my logic?

EDIT: example for my view (the passed data is $data) index.phtml:

<?php echo $this->partial('/partial/show_some_table.phtml', array('data' => $data)); ?>

And the partial (thats how I see it):

foreach ($this->data as $data) {
  if (isset($data['dictionary']) {
    $dic = new \Home\Logic\Dictionary($data['dictionary']); // this thing should return my dictionary which translate given id to text representation
    echo $dic[$data['field']];
  } else {
    echo $data['field'];

Something similar to this.

And the $data['dictionary'] would keep: name of table which should be used to get field text representation, name of id field of dictionary, name of text field of dictionary. So if I pass: array('Cars', 'id', 'name') then my dictionary should know that I want to open table Cars and get as return array of id=>name.


It's not very clear how your app works from these examples, but to answer your question, if Home\Logic\Home needs access to the someTable service, you pass that in as a dependency. Rather than passing the whole service locator in via. the constructor, you tell ZF2 how to create the Home\Logic\Home instance, using a factory.

In your Module.php:

public function getServiceConfig()
    return array(
        'factories' => array(
            'Home\Logic\Home' => function($sm) {
                $someTable = $sm->get('someTable');
                $home = new \Home\Logic\Home($someTable);

                return $home;

Modify the home class to accept that object in its constructor:

namespace Home\Logic;

class Home
    protected $someTable;

    public function __construct($someTable)
        $this->someTable = $someTable;

then in your controller, get the service manager to create the Home\Logic\Home instance for you instead:

public function someAction()
    $logic = $this->getServiceLocator()->get('Home\Logic\Home');

    return ('data' => $logic->getSomeStuff());

now Home\Logic\Home automatically has access to the someTable class.

Need Your Help

HTML5 Audio Player Delete Buffer after played

html html5-audio

I have a simple HTML5 Audio player on my website like this:

How can I pass a UIEvent to a UIScrollView

iphone objective-c uiwindow uiapplication

I am subclassing UIWindow and creating an implementation of sendEvent: as follows: