Beispiel #1
0
 /**
  * Convert to database query
  * @param mixed $data if array given - convert it into string where key - is column name, value - database value to set;
  * if key == "additionalCondition" then we will just add value to string
  * if string givven - just return it without changes
  * @param string $delim delimiter to use in query, recommended - ',', 'AND', 'OR'
  * @return string query string
  */
 public function _getQueryString($data, $delim = ',', $validate = false)
 {
     $res = '';
     if (is_array($data) && !empty($data)) {
         foreach ($data as $k => $v) {
             if (array_key_exists($k, $this->_fields) || $k == $this->_id) {
                 $val = $v;
                 if ($this->_fields[$k]->adapt['dbTo']) {
                     $val = fieldAdapterCsp::_($val, $this->_fields[$k]->adapt['dbTo'], fieldAdapterCsp::DB);
                 }
                 if ($validate) {
                     if (is_object($this->_fields[$k])) {
                         $objForValidation = clone $this->_fields[$k];
                         $objForValidation->setValue($val);
                         if ($errors = validatorCsp::_($objForValidation)) {
                             $this->_addError($errors);
                         }
                     }
                 }
                 /*if(empty($val))
                   $val = $this->_fields[$k]->default;*/
                 switch ($this->_fields[$k]->type) {
                     case 'int':
                     case 'tinyint':
                         $res .= $k . ' = ' . (int) $val . ' ' . $delim . ' ';
                         break;
                     case 'float':
                         $res .= $k . ' = ' . (double) $val . ' ' . $delim . ' ';
                         break;
                     case 'decimal':
                         $res .= $k . ' = ' . (double) $val . ' ' . $delim . ' ';
                         break;
                     case 'free':
                         //Just set it as it is
                         $res .= $k . ' = ' . $val . ' ' . $delim . ' ';
                         break;
                     default:
                         $res .= $k . ' = \'' . $val . '\' ' . $delim . ' ';
                         break;
                 }
             } elseif ($k == 'additionalCondition') {
                 //just add some string to query
                 $res .= $v . ' ' . $delim . ' ';
             }
         }
         $res = substr($res, 0, -(strlen($delim) + 1));
     } elseif (is_string($data)) {
         $res = $data;
     }
     return $res;
 }