Created
May 17, 2011 23:27
-
-
Save redoPop/977656 to your computer and use it in GitHub Desktop.
CakePHP Behavior that stops virtualFields from being selected unless they're explicitly requested.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Behavior to stop CakePHP from including virtual fields unless they're | |
* explicitly requested in the fields array. | |
* | |
* To find out more about virtual fields, consult the documentation in the | |
* manual here: | |
* | |
* http://book.cakephp.org/view/1608/Virtual-fields | |
* | |
* @author joe bartlett (xo@jdbartlett.com) | |
* @license http://www.opensource.org/licenses/mit-license.php MIT License | |
* @package LazyVirtualFielderBehavior | |
*/ | |
class LazyVirtualFielderBehavior extends ModelBehavior { | |
/** | |
* After find callback. Wipes the model's virtual fields array for the next | |
* query. | |
* | |
* @param object $model Model using this behavior | |
* @access public | |
*/ | |
public function afterFind(&$Model) { | |
$Model->virtualFields = array(); | |
} | |
/** | |
* Runs before a Model::find() operation. Examines the query's requested | |
* fields and tailors the model's ::virtualFields property appropriately. | |
* | |
* @param object $Model Model using this behavior | |
* @param array $query Query parameters | |
* @return bool TRUE | |
* @access public | |
*/ | |
public function beforeFind(&$Model, $query) { | |
if (empty($Model->lazyVirtualFields)) { | |
$Model->lazyVirtualFields = $Model->virtualFields; | |
} | |
if (empty($Model->lazyVirtualFields)) { | |
return TRUE; | |
} | |
$Model->virtualFields = array(); | |
$fields = (array)$query['fields']; | |
foreach ($fields as $field) { | |
if ($lazyVirtualField = $this->getLazyVirtualField($Model, $field)) { | |
$Model->virtualFields += $lazyVirtualField; | |
} | |
} | |
return TRUE; | |
} | |
/** | |
* Given a model and field name, returns an array representing a single | |
* virtual field if one is found. | |
* | |
* @param object $Model Model using this behavior | |
* @param string $field Name of a field being requested | |
* @return mixed Boolean FALSE if nothing is found, array otherwise | |
* @access public | |
*/ | |
public function getLazyVirtualField(&$Model, $field) { | |
if (empty($Model->lazyVirtualFields) || !is_string($field)) { | |
return FALSE; | |
} | |
if (isset($Model->lazyVirtualFields[$field])) { | |
return array($field => $Model->lazyVirtualFields[$field]); | |
} | |
if (strpos($field, '.') !== FALSE) { | |
list($model, $field) = explode('.', $field); | |
if ($model == $Model->alias && isset($Model->lazyVirtualFields[$field])) { | |
return array($field => $Model->lazyVirtualFields[$field]); | |
} | |
} | |
return FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment