Skip to content

Instantly share code, notes, and snippets.

@justinyost
Created March 20, 2014 02:42
Show Gist options
  • Save justinyost/9656173 to your computer and use it in GitHub Desktop.
Save justinyost/9656173 to your computer and use it in GitHub Desktop.
Cake2.0 AppModel
<?php
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Model
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Model', 'Model');
App::uses('MspEmail', 'Lib/Network/Email');
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package app.Model
*/
class AppModel extends Model {
/**
* App-wide recursive find.
*
* @var array
*/
//public $recursive = 1;
/**
* App-wide Behaviors.
*
* @var array
*/
public $actsAs = array(
'Containable',
);
/**
* __construct
*
* Sets virtualFields and default sort order dynamically to match the
* model alias. Model names must be backtick-quoted in $this->order and
* $this->VirtualFields entries in order for this automatic replacement
* to take effect! For example: $this->order = '`Model`.field ASC'; will
* be turned into $this->order = '`Alias`.field ASC'; This helps avoid
* the feature accidentally engaging when it shouldn't.
*
* @codeCoverageIgnore Child calls all tested independently.
* @access public
* @params See: http://api.cakephp.org/2.3/source-class-Model.html#641-750
* @return void
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->constructVirtualFields();
$this->constructOrderProperty();
}
/**
* Used by the constructor to make virtualFields dynamically match the
* model alias. Model names must be backtick-quoted in $this->VirtualFields
* entries in order for this automatic replacement to take effect!
*
* @access protected
* @return void
*/
protected function constructVirtualFields() {
foreach ($this->virtualFields as $field => $sql) {
$this->virtualFields[$field] = str_replace("`{$this->name}`", "`{$this->alias}`", $sql);
}
}
/**
* Used by the constructor to make $this->order dynamically match the
* model alias. Model names must be backtick-quoted in $this->order
* for this automatic replacement to take effect! For example:
*
* $this->order = '`Model`.field ASC';
*
* will be turned into
*
* $this->order = '`Alias`.field ASC';
*
* This helps avoid the feature accidentally engaging when it shouldn't.
*
* @access protected
* @return void
*/
protected function constructOrderProperty() {
// Make default sort order(s) more flexible!
if (is_array($this->order)) {
foreach ($this->order as $field => $fieldOrDir) {
if (is_numeric($field)) { // Format is: $this->order = array('Model.field ASC', 'Model.second DESC');
$this->order[$field] = str_replace("`{$this->name}`", "`{$this->alias}`", $fieldOrDir);
} else { // Format is: $this->order = array('Model.field' => 'asc', 'Model.second' => 'desc');
$replacement = str_replace("`{$this->name}`", "`{$this->alias}`", $field);
if ($replacement !== $field) {
$this->order[$replacement] = $fieldOrDir;
unset($this->order[$field]); // Since the key changed, clear the old one.
}
}
}
} elseif (!is_null($this->order)) { // Format is: $this->order = 'Model.field ASC';
$this->order = str_replace("`{$this->name}`", "`{$this->alias}`", $this->order);
}
}
/**
* fullTableName
*
* Provides access to the Model's DataSource's ::fullTableName() method.
* Returns the fully quoted and prefixed table name for the current Model.
*
* @access public
* @param boolean $quote Whether you want the table name quoted.
* @param boolean $schema Whether you want the schema name included.
* @return string Full quoted table name
*/
public function fullTableName($quote = true, $schema = true) {
$datasource = $this->GetDataSource();
return $datasource->fullTableName($this, $quote, $schema);
}
/**
* truncate
*
* Truncates ALL RECORDS from the Model it is called from! VERY DANGEROUS!
* Depends on the ::fullTableName() method to concatenate the configured
* table prefix and table name together.
*
* @access public
* @return mixed
*/
public function truncate() {
$fullName = $this->fullTableName();
$q = 'TRUNCATE TABLE %s;';
return $this->query(sprintf($q, $fullName));
}
/**
* validate_unique
*
* Verifies that there are no other records in the DB with a matching
* value for the field specified in $check. Adds the `NOT [id => x]` element
* into $check if present in $this->data to exclude the existing record
* from the lookup. (Essentially will only return true if any *OTHER*
* records besides this one have a field value matching what's in $check.)
*
* @access public
* @param array $check An array with the field and its value to be validated. Example: array('email' => '[email protected]').
* @param boolean $excludeExisting If true, and if $this->data[Model][id] is present, will exclude that existing record from the check. When this is false, attempting to update an existing record will cause this method to fail (because the existing copy of the record with the matching field IS in the database.)
* @return boolean Returns true if there are no existing records with a matching $check field.
*/
public function validate_unique($check, $excludeExisting = false) {
if (!empty($this->data[$this->alias][$this->primaryKey]) && $excludeExisting) {
$check[] = array(
'NOT' => array(
$this->primaryKey => $this->data[$this->alias][$this->primaryKey]
)
);
}
$options = array(
'conditions' => $check,
'recursive' => -1,
);
return ($this->find('count', $options) === 0);
}
/**
* validate_true
*
* Validation method to ensure a boolean value is true. Used for accepting
* the Terms and Conditions on user registration.
*
* @access public
* @param array $check An array containing the field and its value. Example: array('agreed_to_tos' => true).
* @return boolean Returns true if the value of the only element in $check is boolean true.
*/
public function validate_true($check) {
return (true === (boolean)array_pop($check));
}
/**
* Instantiates and returns an instance of the application's email
* handler class, MspEmail.
*
* @access public
* @param string The name of the CakeEmail config class to use.
* @return MspEmail Instance of the subclassed CakeEmail class.
*/
public function emailFactory($config = null) {
return new MspEmail($config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment