Yii Framework Forum: Invoke Behavior Method Through Php Magic Method __Call Dosent Work - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Invoke Behavior Method Through Php Magic Method __Call Dosent Work

#1 User is offline   Cherif 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 05-June 09
  • Location:Algeria

Posted 29 August 2013 - 10:44 AM

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/
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 31 August 2013 - 11:56 AM

Created a Pull request to add support for this:

https://github.com/y...t/yii2/pull/836
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users