Behavior code
<?php
class DataExportBehavior extends CBehavior
{
public $supportedTypes=array('JSON');
public function __call($name,$parameters)
{
if(preg_match('/^to(\w+)$/', $name, $matches)){
return $this->export($matches[1]);
}else{
parent::__call($name,$parameters);
}
}
public function hello(){
return 'hello';
}
public function export($type)
{
if(!in_array($type, $this->supportedTypes))
throw new CException("Type {$type} is not supported");
if(function_exists('json_encode'))
$encodedCollection=json_encode($this->owner->data);
else
$encodedCollection=CJSON::encode($this->owner->data);
return $encodedCollection;
}
}
Controller action code
public function actionIndex()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$list=new CList;
for($i=0;$i<10;$i++){
$obj=new stdClass;
$obj->id=$i;
$obj->name='Name ' . $i;
$list->add($obj);
}
$dataProvider=new ArrayDataProvider($list->toArray());
$dataProvider->attachBehavior('export','DataExportBehavior');
$this->render('index',array('dataProvider'=>$dataProvider));
}
Invoke the methode in the bahevior but through data provider
$dataProvider->toJSON()
This throw exception that the method dosent if I test the behavior isolated works fine any ideas here? Or we cant use nested __call
Update: sorry this topic should not be here I moved it to :http://www.yiiframework.com/forum/index.php/topic/46631-invoke-behavior-method-through-php-magic-method-call-dosent-work/