phpcakephpcakephp-2.1

Dynamically add virtual field in cakephp


I am using CakePHP 2.1.3; I want to create a virtual field dynamically in a controller. Is it possible? The problem is when I am trying to find max value in a table it gives me another array from the model array. Please ask if you need more information.

When I am trying to execute the following query,

$find_max_case_count = $this->CaseMaster->find('first', array(
    'conditions' => array(
        'CaseMaster.CLIENT_ID' => $client_id,
        'CaseMaster.CASE_NO LIKE' => '%-%'
    ),
    'fields' => array('max(CaseMaster.CASE_NO) AS MAX_NO')
));

It is giving me an array like:

[0]=> array([MAX_NO]=> 52)

However I want it to be like as:

[CaseMaster] => array([MAX_NO] => 52)

Solution

  • I found a solution. I can make the virtual field at runtime. The code should looks like:

    $this->CaseMaster->virtualFields['MAX_NO'] = 0;
    

    Write it just above the find query and the query will remain same as it was written. This link was helpful to find out the solution.