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

Creating virtual fields
Creating virtual fields is easy. In each model you can define a $virtualFields property that contains
an array of field => expressions. An example of a virtual field definition using MySQL would be:
public $virtualFields = array(
’name’ => ’CONCAT(User.first_name, " ", User.last_name)’
);
And with PostgreSQL:
public $virtualFields = array(
’name’ => ’User.first_name || \’ \’ || User.last_name’
);
In subsequent find operations, your User results would contain a name key with the result of the concatenation.
It is not advisable to create virtual fields with the same names as columns on the database, this can
cause SQL errors.
It is not always useful to have User.first_name fully qualified. If you do not follow the convention (i.e. you
have multiple relations to other tables) this would result in an error. In this case it may be better to just use
first_name || \’ \’ || last_name without the Model Name.
Using virtual fields
Creating virtual fields is straightforward and easy, interacting with virtual fields can be done through a few
different methods.
Model::hasField()
Model::hasField() will return true if the model has a concrete field passed by the first parameter. By setting
the second parameter of hasField() to true, virtualFields will also be checked when checking if a model has
a field. Using the example field above:
$this->User->hasField(’name’); // Will return false, as there is no concrete field called name
$this->User->hasField(’name’, true); // Will return true as there is a virtual field called Model::isVirtualField()
This method can be used to check if a field/column is a virtual field or a concrete field. Will return true if
the column is virtual:
$this->User->isVirtualField(’name’); //true
$this->User->isVirtualField(’first_name’); //false

 
 
 

osdyui

Skills    Cakephp

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

  Students (0)