Bug/improvement to allow PHP code evaluation in CDataColumn

Hi,

In CDataColumn, one can enter for ‘value’ a simple PHP code snippet for dynamic evaluation with the “current” record in context. That’s very useful.

However, the code snippet is very limited due to the fact that in CComponent::evaluateExpression its being evaluated with a prefixed 'return ’ … keyword. Meaning, the code snippet cannot contain two statements for example.

What needs to be changed to a allow a little more flexibility in the code snippet that is to run is to change the CDataColumn::evaluateExpression() (which is an override of CComponent::evaluateExpression()) to be as follows:




public function evaluateExpression($_expression_, $_data_ = array()) {


	if (is_string($_expression_)) {

		extract($_data_);

		return eval($_expression_ . ';');

	}

	else {

		$_data_[] = $this;

		return call_user_func_array($_expression_, $_data_);

	}

}



The only change from CComponent method is the removal of the preceeding [color="#0000ff"]'return '.[/color] text in the string passed to eval(). Doing so indeed requires the users that write the code snippet to add “return” in the appropriate place but this price is IMHO worth the pay for this flexibility.

I’m not sure if this is a bug or a feature request though.

Thanks,

Boaz.

This would break BC… on PHP 5.3 you can use anonymous functions… and on previous versions you can create custom methods in the model and call them here…

Right.

I’ll create a simple extension for this, “for whoever it might concern…” :)

Extension released: http://www.yiiframework.com/extension/pcphpdatacolumn/