Skip to content

Instantly share code, notes, and snippets.

@jaredhoyt
Created January 11, 2011 21:14
Show Gist options
  • Save jaredhoyt/775156 to your computer and use it in GitHub Desktop.
Save jaredhoyt/775156 to your computer and use it in GitHub Desktop.
<?php
class Contact extends AppModel {
var $actsAs = array('Acl' => array('requester'));
var $belongsTo = array(
'Account',
'Role',
'Supervisor' => array(
'className' => 'Contact',
'foreignKey' => 'supervisor_id'
)
);
var $displayField = 'abbr_name';
var $hasMany = array(
'Notice' => array('foreignKey' => 'contact_id'),
'Document' => array('foreignKey' => 'created_by')
);
var $_findMethods = array('accessible' => true, 'roles' => true);
function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields = array(
'full_name' => "CONCAT(`{$this->alias}`.`first_name`, ' ', `{$this->alias}`.`last_name`)",
'directory_name' => "CONCAT(`{$this->alias}`.`last_name`, ', ', `{$this->alias}`.`first_name`)",
'abbr_name' => "CONCAT(LEFT(`{$this->alias}`.`first_name`, 1), '. ', `{$this->alias}`.`last_name`)"
);
}
<?php
class UserTrackableBehavior extends ModelBehavior {
function setup(&$model, $settings = array()) {
$this->settings[$model->alias] = array_merge(array('userModel' => 'User'), $settings);
# Initialize user model
if (empty($this->userModel[$model->alias])) {
$this->userModel[$model->alias] =& ClassRegistry::init($this->settings[$model->alias]['userModel']);
}
# Cache user ids for translation
if (empty($this->userList[$this->settings[$model->alias]['userModel']])) {
$this->userList[$this->settings[$model->alias]['userModel']] = $this->userModel[$model->alias]->find('list', array('callbacks' => false));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment