Add virtual field in cakephp 2.* in find methods

| By Webner

How to specify an extra field (not existing in the table) while fetching data from database using find method? Solution is explained below.

Assign value to virtual field before find function call:

$this->virtualFields['virtual_field_name'] = 'any value';

Use virtual field in find function:

$result = $this - > find('all',
        'fields' => array(
            'ModelName.virtual_field_name', //user virtual field
    )); 	 unset($this->virtualFields[‘virtual_field_name’]);

Leave a Reply

Your email address will not be published. Required fields are marked *