简体   繁体   English

Zend2-如何访问不在控制器中的模型

[英]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: 我的zend2应用程序将一些功能移到了其他来源,例如,我的控制器如下所示:

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. 我喜欢这种构建应用程序的方式,因为在Zend1中我曾经用这种方式来执行此操作,这使我的代码非常简单,易于阅读/编辑。 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. 在Zend2中,我有很多问题-首先-我必须在逻辑构造函数中推送serviceLocator-这对我来说是非常糟糕的解决方案。 :( 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: :(有时出于多种原因,我需要访问视图中的模型(例如,为了轻松访问字典字段,这意味着在表所有者中,我具有id_car,该id_car链接到表汽车。我不想创建视图为此,但只需指出我的模型所有者,以便在视图中读取另一个模型即可使用该字段...现在让我们看一下我的逻辑:

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? 我的问题是-如何在不将serviceLocator从控制器推入逻辑的情况下访问我的模型?

EDIT: example for my view (the passed data is $data) index.phtml: 编辑:我的视图的示例(传递的数据是$ data)index.phtml:

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

And the partial (thats how I see it): 和部分(这就是我的看法):

$this->placeHolder('table-body')->captureStart();
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'];
  }
}
$this->placeHolder('table-body')->captureEnd();

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. $ data ['dictionary']将保留:应用于获取字段文本表示形式的表的名称,字典的id字段的名称,字典的文本字段的名称。 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. 因此,如果我通过:array('Cars','id','name'),那么我的字典应该知道我要打开表Cars并获取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. 从这些示例来看,您的应用程序的工作方式还不是很清楚,但是要回答您的问题,如果Home\\Logic\\Home需要访问someTable服务,您可以将其作为依赖项传入。 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. 构造函数,您告诉ZF2如何使用工厂创建Home\\Logic\\Home实例。

In your Module.php: 在您的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: 修改home类以在其构造函数中接受该对象:

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: 然后在您的控制器中,让服务管理器为您创建Home\\Logic\\Home实例:

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. 现在Home\\Logic\\Home自动访问someTable类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM