Cakephp Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86|Last
Lessons:-Models

Model::getVirtualField()
This method can be used to access the SQL expression that comprises a virtual field. If no argument is
supplied it will return all virtual fields in a Model:
$this->User->getVirtualField(’name’); //returns ’CONCAT(User.first_name, ’ ’, User.last_name)’
Model::find() and virtual fields
As stated earlier Model::find() will treat virtual fields much like any other field in a model. The value
of a virtual field will be placed under the model’s key in the resultset:
$results = $this->User->find(’first’);
// results contains the following
array(
’User’ => array(
’first_name’ => ’Mark’,
’last_name’ => ’Story’,
’name’ => ’Mark Story’,
//more fields.
)
);
Pagination and virtual fields
Since virtual fields behave much like regular fields when doing find’s, Controller::paginate() will
be able to sort by virtual fields too.
Virtual fields and model aliases
When you are using virtualFields and models with aliases that are not the same as their name, you can run
into problems as virtualFields do not update to reflect the bound alias. If you are using virtualFields in
models that have more than one alias it is best to define the virtualFields in your model’s constructor:
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields[’name’] = sprintf(’CONCAT(%s.first_name, " ", %s.last_name)’, $this->}
This will allow your virtualFields to work for any alias you give a model.
Virtual fields in SQL queries
Using functions in direct SQL queries will prevent data from being returned in the same array as your
model’s data. For example this:

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)