CakePHP Virtual Fields

Leave a Comment
CakePHP virtual fields property enables us to add imaginary fields to the Model like we are using concat fields SQL query.

select first_name, last_name, concat(first_name, ' ' , last_name) as name from users;

To define Virtual Fields in CakePHP Model:

public $virtualFields = array(
   'name' => "CONCAT(User.first_name, ' ', User.last_name)"
); 

When use $this->User->find('all') or $this->User->find('first') on user model, This user model will generate query like this

select first_name, last_name, concat(first_name, ' ' , last_name) as name from users;

And the ouput will be

CakePHP Virtual Fields
CakePHP Virtual Fields

0 comments:

Post a comment