Reputation: 1478
Hay. Currently I'm creating a function in Behaviour in CakePHP 2.10.12 as shown below :
<?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);
}
}
?>
Then above behavior is used by my model as shown below :
<?php
class Test extends AppModel {
public $actsAs = array('Converter');
}
And then I call the function that I created from behavior in my Controller as shown below :
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));
}
The problem is initConverter
cannot be initialized. I check the variabel that are inputed from controller and all of those variabel are null (it's weird). But when I call the hellow(
) (function in behavior), the result is displayed in my view. So is there something missing here ?
Thank you
Note :
This is the error message displayed in my view :
Upvotes: 0
Views: 427
Reputation: 60463
Look at the warning/notice, you are receiving an object where you expect a string/number.
The first argument of an externally invoked behavior method will always be the instance of the model that the behavior is attached to, ie your method signatures should be like:
function initConverter(Model $model, $locale, $timezone, $currency)
function convertCurrency(Model $model, $currencyAmount)
// etc...
See also
Upvotes: 1