Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Created April 29, 2014 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/0939f976a8827dba753b to your computer and use it in GitHub Desktop.
Save angelxmoreno/0939f976a8827dba753b to your computer and use it in GitHub Desktop.
Advance use of CakePHP Virtual Fields
/**
* Constructor. Binds the model's database table to the object.
*
* @param integer|string|array $id Set this ID for this model on startup, can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
//verify that all children have a $role
$this->virtualFields['name'] = sprintf('CONCAT(%s.first_name, " ", %s.last_name)', $this->alias, $this->alias);
$this->virtualFields['initials'] = sprintf('CONCAT(UPPER(LEFT(%s.first_name,1)), UPPER(LEFT(%s.last_name,1)))', $this->alias, $this->alias);
$this->virtualFields['last_schoolclass_visited'] = sprintf('(SELECT `user_settings`.`last_class_visited` FROM `user_settings` where `user_settings`.`user_id` = `%s`.`id`)', $this->alias);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment