Zend framework2 ZendFramework 2-如何从模型类调用zfcUser

Zend framework2 ZendFramework 2-如何从模型类调用zfcUser,zend-framework2,Zend Framework2,我试图从模型中的类调用ZFCUser,但我一直收到以下消息: Call to undefined method Members\Model\MemberTable::zfcUserAuthentication() 我试着这样称呼它: public function getUserEntity() { if($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {

我试图从模型中的类调用ZFCUser,但我一直收到以下消息:

Call to undefined method Members\Model\MemberTable::zfcUserAuthentication()
我试着这样称呼它:

public function getUserEntity()
    {
        if($this->zfcUserAuthentication()->getAuthService()->hasIdentity())
        {
            if (!$this->user_entity) 
            {
                $this->setUserEntity($this->zfcUserAuthentication()->getAuthService()->getIdentity());
            }
            return $this->user_entity;
        }
    } 
我怀疑我需要实现/扩展一个类,以便重新编码ZFcuser

我真的很想得到一些快速的建议

多谢各位

顺便说一下

当我从我的控制器调用ZFCUser时,它就会工作,因此,使用Zend\Mvc\controller\AbstractActionController显然可以重新编码它


但是,对于框架中的其他类,什么是AbstractActionController的等价物呢

zfcUserAuthentication()
是一个控制器插件,因此可以从控制器调用它。您不能(或不应该)尝试从非控制器访问此。如果您需要另一个类中的用户实体,则应将其作为该类的依赖项传入

编辑:更新成员表工厂,使其设置用户实体:

'Members\Model\MemberTable' => function($sm) {
    $tableGateway = $sm->get('MemberTableGateway');
    $table = new MemberTable($tableGateway);

    $authService = $serviceLocator->get('zfcuser_auth_service');
    $userEntity = $authService->getIdentity();

    $table->setUserEntity($userEntity);

    return $table;
}
将属性添加到
MemberTable
类中:

protected $userEntity;
以及它的获取者/设置者:

public function setUserEntity($userEntity)
{
    $this->userEntity = $userEntity;
}

public function getUserEntity()
{
    return $this->userEntity;
}

然后在需要时调用
$this->getUserEntity()

有多种方法可以做到这一点-

方法之一是在“MemberTable.php”文件中执行以下操作。
也许我们的项目中已经提供了下面大部分代码行。

A.在“namespace”语句后添加以下行-

use Zend\ServiceManager\ServiceLocatorAwareInterface;         //Added Line
use Zend\ServiceManager\ServiceLocatorInterface;              //Added Line 
class MemberTable implements ServiceLocatorAwareInterface {
     protected $serviceLocator;                                //Added line
     ....
     .....
}
b。将class语句更改为-

class MemberTable implements ServiceLocatorAwareInterface {    //Modified Line
     ....
     .....
}
c。在class语句的顶部添加以下行-

use Zend\ServiceManager\ServiceLocatorAwareInterface;         //Added Line
use Zend\ServiceManager\ServiceLocatorInterface;              //Added Line 
class MemberTable implements ServiceLocatorAwareInterface {
     protected $serviceLocator;                                //Added line
     ....
     .....
}
d。添加以下函数-

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
    $this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
    return $this->serviceLocator;
}
e。在Members/Module.php中进行更改

'Members\Model\MemberTable' => function($sm) {
    $tableGateway = $sm->get('MemberTableGateway');
    $table = new MemberTable($tableGateway);
    $table->setServiceLocator($sm);                   //This is the important line.
    return $table;
}

现在,MemberTable类中提供了“ServiceLocator”。
要访问
zfcUserAuthentication()
或任何控制器插件,可以执行以下操作-

public function getUserEntity()
{
    $zfcUserAuth = $this->getServiceLocator()->get('controllerPluginManager')->get('zfcUserAuthentication');

    if($zfcUserAuth->getAuthService()->hasIdentity())
    {
        if (!$this->user_entity) 
        {
            $this->setUserEntity($zfcUserAuth->getAuthService()->getIdentity());
        }
        return $this->user_entity;
    }
}

你好,蒂姆。谢谢你的建议,我现在明白了。快速提问。当你说依赖时,你会建议如何将这个值输入到另一个模块中?您好,Tim Kunal似乎对如何从模型访问服务定位器给出了很好的建议。请允许我问一下,为什么您认为从模型中访问它是不可取的。我仍在努力学习与Zend合作的正确方法。因此,我非常感谢其他人对这些问题的看法她建议您将整个服务定位器注入MemberTable,这样您就可以使用它来获取控制器插件管理器,并使用它来获取zfc用户身份验证插件,然后使用它来获取身份验证服务,然后使用它来获取用户实体。为什么不首先将用户实体注入MemberTable?这更容易做到,代码更清晰,测试也更容易。我已经更新了我的答案。嘿,蒂姆。谢谢你的回答。它确实看起来更简单,称之为你的方式。最后一个问题。我是否仍然需要使用:class MemberTable实现ServiceLocatorAwareInterface{//Modified Line………}不,您不需要
ServiceLocatorAwareInterface
。您所做的只是调用您在类中定义的
setUserEntity()
方法不应该是必需的,因为服务定位器感知初始值设定项应该为您调用它。hi Kunal。谢谢你的密码。快速提问。在e中,什么是$tableGateway=$sm->get('MemberTableGateway');。我的意思是,什么是“MemberTableGateway”,我使用TableGateway而不是任何ORM(如条令等)来执行我的查询。因此,我必须将TableGateway对象的依赖项注入到我用于查询的ModelTable中(通过构造函数)。在您的例子中,是“MemberTable”类。您可能需要也可能不需要这样的对象注入。(我提到的是TableGateway的默认用法)对于ref: