How to use multidimensional objects as active record in JasperPHP

In our case, active record objects are a PHP class object to give super powers to a returned record in database recordset, the magic methods “__get” and “__set” act as router intercepting read and write fields and runs any code than you need.
Like this sample:

public class ActRecordTest{

public function _get($prop) {
if (method_exists($this, 'get_'.$prop)) // if an method defined with get+name of propriety exits, run it
{
// run method get_ + name of propriety
return call_user_func(array($this, 'get_'.$prop));
}
else // fallack to return a value into propriety, in this case a field value
{
// return a value of propriety
if (isset($this->$prop))
{
return ($this->$prop);
}
}
}
}

To use this features, create a report and add this steps to get a powerfull of active record into yours reports

1 . Add a variable named ‘recordObj’ in your report and set ‘Initial value expression’ to a PHP className.

2 . Create two classes

public class ActRecordTest{
public function _get($prop) {
if (method_exists($this, 'get_'.$prop)) // if an method defined with get+name of propriety exits, run it
{
// run method get_ + name of propriety
return call_user_func(array($this, 'get_'.$prop));
}
else // fallack to return a value into propriety, in this case a field value
{
// return a value of propriety
if (isset($this->$prop))
{
return ($this->$prop);
}
}
}
public function get_a_second_object{
return new A_second_object();
}
public function get_sameCodeintoThis(){
//same code
return of same code;
}

public class A_second_object{
public function _get($prop) {
if (method_exists($this, 'get_'.$prop)) // if an method defined with get+name of propriety exits, run it
{
// run method get_ + name of propriety
return call_user_func(array($this, 'get_'.$prop));
}
else // fallack to return a value into propriety, in this case a field value
{
// return a value of propriety
if (isset($this->$prop))
{
return ($this->$prop);
}
}
}
public function get_calculatedValue(){
return $this->field1 + $this->field2;
}
public function get_sameCodeintoThis(){
//same code
return result of same code;
}
}

In this sample, a jasper TextField object with expression “$F{A_second_object->calculatedValue}” return a result of field1+field2

Rogério Castro

System analyst and co-owner of ciatec.net and quilhasoft systems

You may also like...

Popular Posts