ZF2:从类中的不同类访问方法

时间:2013-06-06 10:43:06

标签: php zend-framework2

在“事件”类中,我想将我的班级“客户”中的变量“customername”分配给其中一个类变量。我的CustomerTable类有一个名为'getCustomer'的方法,它通过传递customerId来创建Customer实例。我想从我的Event类中调用该方法,以便我可以从该客户获取客户名称并将其分配给Event。

我已经尝试使用事件扩展AbstractActionController并使用服务管理器来获取方法,如下面的示例所示。但是当我尝试在我的CustomerTable类上调用get()时,会触发一条消息,说我试图在非对象上调用get()。

基本上:有没有人知道如何从我的Event类中调用getCustomer()?

我认为我的事件类:

class Event
{
    public $id;
    public $CCID;
    public $customerName;   
    public $jobId;
    public $timeServer;
    public $timeConvert;
    public $messageId;
    public $message = null;
    public $severity;
    public $client;

    public function exchangeArray($data)
    {
        $this->id       = (isset($data['id'])) ? $data['id'] : null;
        $this->CCID     = (isset($data['CCID'])) ? $data['CCID'] : null;
        // here's the stuff that tries to get the getCustomer() method
        $sm                 = $this->getServiceLocator();
        $customer            = $sm->get('Admin\Model\CustomerTable')->getCustomer($this->CCID);
        $this->customerName = $customer->customerName;
        $this->jobId    = (isset($data['jobId'])) ? $data['jobId'] : null;
        $this->timeServer   = (isset($data['TimeServer'])) ? $data['TimeServer'] : null;
        $this->messageId    = (isset($data['messageId'])) ? $data['messageId'] : null;
        $this->severity     = (isset($data['Severity'])) ? $data['Severity'] : null;
        $this->client       = (isset($data['Client'])) ? $data['Client'] : null;
        $this->timeConvert  = gmdate("H:i", $this->timeServer);
    }
}

1 个答案:

答案 0 :(得分:1)

以下是两个如何加载课程的示例:

use \Admin\Model\CustomerTable; // use namespaces
$sm = new CustomerTable();
$customer = $sm->getCustomer($this->CCID);

或者像

一样
$sm = new \Admin\Model\CustomerTable\Tidy();
$customer = $sm->getCustomer($this->CCID);