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

Call Specific Function From Behavior CakePHP 2.10.12 Not Working

干草。目前我正在 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');
}

然后调用我在控制器中根据行为创建的函数,如下所示:

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() (行为中的函数)时,结果显示在我的视图中。那么这里缺少什么吗? 谢谢

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

查看 warning/notice,您收到了一个您希望 string/number 的对象。

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

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

function convertCurrency(Model $model, $currencyAmount)

// etc...

另见