从行为调用特定函数CakePHP 2.10.12不工作

问题描述 投票:0回答:1

干草。目前我正在CakePHP 2.10.12中的Behavior中创建一个函数,如下所示:

<?php
App::uses('CakeTime', 'Utility');
App::uses('CakeNumber', 'Utility');
class ConverterBehavior extends ModelBehavior {
    private $timezone;
    private $locale;
    private $currency;

    function hellow() {
        return "Hellow from behavior";
    }

    function initConverter($locale, $timezone, $currency) {
        $this->locale = $locale;
        $this->timezone = $timezone;
        $this->currency = $currency;
        setlocale(LC_ALL, $locale);
    }

    public function getCurrentLocale() {
        return $this->locale;
    }

    function convertCurrency($currencyAmount) {
        return CakeNumber::currency($currencyAmount, $this->currency);
    }

    function convertDate($date) {
        return CakeTime::i18nFormat($date, null, false, $this->timezone);
    }

}
?>

然后我的模型使用上面的行为,如下所示:

<?php

class Test extends AppModel {
    public $actsAs = array('Converter');
}

然后我调用我在Controller中的行为创建的函数,如下所示:

public function converterModel() {
    $this->Test->initConverter('ja_JP', 'Asia/Tokyo', 'JPY');
    $temp = $this->Test->convertCurrency(23456789901.123456);
    debug($this->Test->hellow());
    // $this->set('jpDate', $this->Test->convertDate(new DateTime));
}

问题是initConverter无法初始化。我检查从控制器输入的变量,并且所有变量都是空的(这很奇怪)。但是当我调用hellow((行为中的函数)时,结果会显示在我的视图中。那么这里缺少什么?谢谢

注意:这是我视图中显示的错误消息:enter image description here

cakephp behavior cakephp-2.x
1个回答
1
投票

查看警告/通知,您将收到一个您希望有字符串/数字的对象。

外部调用行为方法的第一个参数将始终是行为附加到的模型的实例,即您的方法签名应如下所示:

function initConverter(Model $model, $locale, $timezone, $currency)

function convertCurrency(Model $model, $currencyAmount)

// etc...

也可以看看

© www.soinside.com 2019 - 2024. All rights reserved.