Skip to content

Instantly share code, notes, and snippets.

@cappert
Created September 18, 2012 09:00
Show Gist options
  • Select an option

  • Save cappert/3742157 to your computer and use it in GitHub Desktop.

Select an option

Save cappert/3742157 to your computer and use it in GitHub Desktop.
CakePHP postConditions
/**
* 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 Cake.Controller
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
*
* @param array $data POST'ed data organized by model and field
* @param string|array $op A string containing an SQL comparison operator, or an array matching operators
* to fields
* @param string $bool SQL boolean operator: AND, OR, XOR, etc.
* @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be
* included in the returned conditions
* @return array An array of model conditions
* @deprecated
*/
public function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
if (!is_array($data) || empty($data)) {
if (!empty($this->request->data)) {
$data = $this->request->data;
} else {
return null;
}
}
$cond = array();
if ($op === null) {
$op = '';
}
$arrayOp = is_array($op);
foreach ($data as $model => $fields) {
foreach ($fields as $field => $value) {
$key = $model . '.' . $field;
$fieldOp = $op;
if ($arrayOp) {
if (array_key_exists($key, $op)) {
$fieldOp = $op[$key];
} elseif (array_key_exists($field, $op)) {
$fieldOp = $op[$field];
} else {
$fieldOp = false;
}
}
if ($exclusive && $fieldOp === false) {
continue;
}
$fieldOp = strtoupper(trim($fieldOp));
if ($fieldOp === 'LIKE') {
$key = $key . ' LIKE';
$value = '%' . $value . '%';
} elseif ($fieldOp && $fieldOp != '=') {
$key = $key . ' ' . $fieldOp;
}
$cond[$key] = $value;
}
}
if ($bool != null && strtoupper($bool) != 'AND') {
$cond = array($bool => $cond);
}
return $cond;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment