Source for file sql_compatible.php
Documentation is available at sql_compatible.php
* Attach this behavior to be able to query mongo DBs without using mongo specific syntax.
* If you don't need this behavior don't attach it and save a few cycles
* Copyright (c) 2010, Andy Dawson
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
* @copyright Copyright (c) 2010, Andy Dawson
* @subpackage mongodb.models.behaviors
* @since v 1.0 (24-May-2010)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* SqlCompatibleBehavior class
* @subpackage mongodb.models.behaviors
* @var string 'SqlCompatible'
public $name =
'SqlCompatible';
* defaultSettings property
* Allow overriding the operator map
* @param array $config array()
public function setup(&$Model, $config =
array()) {
* If requested, convert dates from MongoDate objects to standard date strings
public function afterFind(&$Model, $results, $primary) {
if ($this->settings[$Model->alias]['convertDates']) {
* If conditions are an array ensure they are mongified
* Convert MongoDate objects to strings for the purpose of view simplicity
foreach($results as &$row) {
} elseif (is_a($results, 'MongoDate')) {
$results =
date('Y-M-d h:i:s', $results->sec);
* translateConditions method
* Loop on conditions and desqlify them
* @param mixed $conditions
foreach($conditions as $key =>
&$value) {
if (substr($uKey, -
5) ===
'NOT IN') {
// 'Special' case because it has a space in it, and it's the whole key
$conditions[substr($key, 0, -
5)]['$nin'] =
$value;
unset
($conditions[$key]);
unset
($conditions[$key]);
foreach($value as $key =>
$part) {
$part =
array($key =>
$part);
$conditions['$or'][] =
$part;
if ($key ===
$Model->primaryKey &&
is_array($value)) {
//_id=>array(1,2,3) pattern, set $in operator
$isMongoOperator =
false;
foreach($value as $idKey =>
$idValue) {
//check a mongo operator exists
if(substr($idKey,0,1) ===
'$') {
if($isMongoOperator ===
false) {
$conditions[$key] =
array('$in' =>
$value);
if (substr($uKey, -
3) ===
'NOT') {
// 'Special' case because it's awkward
$conditions[$childKey]['$nin'] = (array)
$childValue;
unset
($conditions['NOT']);
$conditions[$childKey]['$not'][$operator] =
$childValue;
unset
($conditions['NOT']);
if (substr($uKey, -
5) ===
' LIKE') {
// 'Special' case because it's awkward
if (substr($value, -
1) ===
'%') {
$value =
substr($value, 0, -
1);
$conditions[substr($key, 0, -
5)] =
new MongoRegex("/$value/i");
unset
($conditions[$key]);
$conditions[$newKey][$operator] =
$value;
unset
($conditions[$key]);
* translateOperator method
* Use the operator map for the model and return what the db really wants to hear
if (!empty($this->settings[$Model->alias]['operators'][$operator])) {
return $this->settings[$Model->alias]['operators'][$operator];
Documentation generated on Tue, 26 Jul 2011 01:09:02 +0900 by phpDocumentor 1.4.3