Hi Readers,
Today I will be discussing about the limitation of virtualfields and how can we overcome it's limitations. Firstly, we can not use virtualfields in associated models for conditions,fields arrays or order. Doing so will generally an SQL error, this is because it is difficult to estimate the actual depth at which an associated model might be found.
To overcome it we can copy virtualFields from one model to another at run time when ever you need to:
Syntax:-
First Step:- Create virtual fields in a model. I have added the following code in my Modal named UserProfile.
public $virtualFields = array(
'full_name' => 'CONCAT(UserProfile.firstname, " ", UserProfile.lastname)'
);
Second Step:- In your Controller copy virtualFields from one model to another. Write the following code in your controller to accomplish it.
$this->Score->virtualFields['full_name'] = $this->UserProfile->virtualFields['full_name'];
$finalList=$this->Score->find('list',array('joins' => $gymnastjoins,'conditions' => array('Score.eventid' => $eventid), 'fields' => array('UserProfile.user_id','Score.full_name')));
0 Comment(s)