Cakephp Classroom image

Anil  Bist / Professional / Web Technology

 
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:

 
 
 
image
Anil  Bist

Skills    Cakephp

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)